[go: up one dir, main page]

0% found this document useful (0 votes)
264 views34 pages

Csc186topic 1 - Part2

The document discusses object-oriented programming concepts including classes, objects, and their elements. It defines a class as a template that defines an object's attributes and behaviors. An object is an instance of a class that contains specific attribute values and states. The document provides examples of classes like Vehicle and objects like a specific car. It also covers messages, methods, and how objects communicate through passing messages that methods can handle.

Uploaded by

Nurain Damia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
264 views34 pages

Csc186topic 1 - Part2

The document discusses object-oriented programming concepts including classes, objects, and their elements. It defines a class as a template that defines an object's attributes and behaviors. An object is an instance of a class that contains specific attribute values and states. The document provides examples of classes like Vehicle and objects like a specific car. It also covers messages, methods, and how objects communicate through passing messages that methods can handle.

Uploaded by

Nurain Damia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

CSC186 OBJECT ORIENTED PROGRAMMING

CSC186 OBJECT ORIENTED PROGRAMMING| edited from JUHAIDA ISMAIL 1


LESSON OUTCOMES
Upon completion of this chapter, students should be able to:

▪ Name the basic components of object-oriented programming


▪ Differentiate classes and objects.
▪ Name the elements of an object.
▪ Describe messages and methods (message passing).
▪ Describe the OOP characteristics.

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL 2


INTRODUCTION TO OBJECTS
DEFINITION OF OBJECT

▪ Object-oriented programs use objects.


▪ An object is a thing, both tangible and intangible.

TYPE EXAMPLE
Tangible (can be seen & touched) Vehicle, Employee, Clock
Intangible (conceptual) Account, Loan, Time, Date

▪ To create an object inside the computer program, we must provide


a definition for objects—how they behave and what kinds of
information they maintain —called a class.

▪ An object is an instance of class.

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL


INTRODUCTION TO OBJECTS
ELEMENTS OF AN OBJECT
▪ Each object contains data and operations to manipulate the data.
✓ operations are referred as methods or member functions
✓ data are referred as properties, attributes or member data.
▪ Elements of an object :

ELEMENT DEFINITION
Attribute Data associated with object
Behavior Action performed by an object (Operation)
Defined by methods
State The description (current value) of the attribute.

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL


INTRODUCTION TO OBJECTS
ELEMENTS OF AN OBJECT
For example, you want to write a program for video rental. The objects
involved are video and customer. This segment uses VIDEO as the object.
Object have three elements:

1. Attribute (data related to the object)


For the video object, the data or attributes might be movie name,
starring actors, producer name, production name and number of copies
in stock
2. Behaviour (operations on a video object)
For example, Checking the name of movie, reducing the number of
copies in stock by one after a copy is rented and incrementing the
number of copies in stock by one after a customer returns a copy.
3. State (current situation)

For the video object, the state might be movie name=Hantu Siang,
starring actors=Mira Filzah,producer name=Kayangan Production, number
of copies in stock=30
INTRODUCTION TO OBJECTS
ELEMENTS OF AN OBJECT (Try this out ☺)

What are the attributes, states and behaviour of this object?


INTRODUCTION TO OBJECTS
ELEMENTS OF AN OBJECT
What are the attributes, states and behaviour of this object?

Attributes Behaviour
▪ Plat Number ▪ Move forward
▪ Model ▪ Reverse
▪ Color ▪ Stop
▪ Engine Size

States
▪ KCR 260
▪ Beetle
▪ Metallic Grey
▪ 2.0
INTRODUCTION TO CLASS
DEFINITION OF CLASS
▪ A class is a template, blueprint or contract that defines what
attributes (data fields) its object will have and what those objects will
be able to do (methods).
▪ A class is a collection of objects with common properties.

OBJECT CLASS
Apple
Mango FRUIT
Orange

✓ It is a user defined data type.


✓ Associate with each class is a set of properties (data) and
✓ behaviors (functions).
✓ Objects are variables of classes.

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL


INTRODUCTION TO CLASS
DEFINITION OF CLASS

▪ A CLASS is an essentially a description of how to create an object

✓ A class must be defined before you can create an instance


(object) of the class.

✓ Creating an instance is referred to as instantiation.

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL


UNDERSTANDING OBJECTS & CLASSES

CAT (class) Breed : British Shorthair


Size: Small
Age: 2 years
Breed Color: Grey

Size
Age
Color (object) Breed : Bengal
Size: Medium
Age: 4 years
Eat( ) Color: Brown
Sleep( )
Sit( )
Run( )
Breed : Persian
Size: Large
Age: 3 years
Color: Dark Brown
UNDERSTANDING OBJECTS & CLASSES
OBJECT ORIENTED DESIGN (EXAMPLE 1)

Problem statement
▪ Write a program to input the length and width of a rectangle, and
calculate and print the perimeter and area of the rectangle

Nouns:
✓ length
✓ width
✓ rectangle
✓ perimeter
✓ area

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL


UNDERSTANDING OBJECTS & CLASSES
OBJECT ORIENTED DESIGN (EXAMPLE 1)
Class Rectangle with 2 data members and 7 operations

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL


UNDERSTANDING OBJECTS & CLASSES
OBJECT ORIENTED DESIGN (EXAMPLE 2)

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL


UNDERSTANDING OBJECTS & CLASSES
UML CLASS DIAGRAM (EXAMPLE 2)

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL


UNDERSTANDING OBJECTS & CLASSES
CLASS vs. OBJECT

CLASS OBJECT
A class is a template for An object is a member or an
objects. "instance" of a class.
A class defines object An object has a state in
properties including a valid which all of its properties
range of values, and a have values that you either
default value. explicitly define or that are
defined by default settings.
A class also describes object
behavior.

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL


MESSAGES & METHODS

▪ To instruct a class or an object to perform a task, we send a


message to it.

▪ You can send a message only to the classes and objects that
understand the message you sent to them.

▪ Calling a specific method in an object is described as sending


the object a message.

▪ A class or an object must possess a matching method to be able


to handle the received message.
CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL
MESSAGES & METHODS
▪ For example if “CUSTOMER” and “ACCOUNT” are two objects in a
program, then the customer object may send a message to the
account object requesting for the bank balance.

✓ A method defined for a class is called a CLASS METHOD, and


a method defined for an object is called an INSTANCE
METHOD.

✓ A value we pass to an object when sending a message is


called an ARGUMENT of the message.

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL


MESSAGES & METHODS
MESSAGE PASSING (Customer & Account)

Messages enter PIN NUMBER

verify

request withdrawal

dispense money

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL


EXAMPLE 1

Cemerlang Tution Center request for Automate Billing


System. The bill will be calculate based on number of
subject taken. Data of the students that will store are
the student’s name, IC number, contact number,
address, and number of subject taken.

✓ As the first step in the design process, identify the


objects and their attributes for the system. (DO NOT
write a class for this problem)

**The answer will be discuss in the class

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL


EXERCISE 1

The Center of Islamic Kids wants to automate the


process of storing student’s information. You have
been given the task of designing the system. The
center normally stores information such as the
student’s name, date of birth, parent’s name, parent’s
contact number, address, and medical data.

✓ As the first step in the design process, identify the


objects and their attributes for the system. (DO NOT
write a class for this problem)

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL


EXERCISE 2

The following is the situation of football club at UiTM


Raub. They plan to store and automate information
related to the club. You were assigned to design the
system. The information is related to teams and players
for an every competition they involved. Every single
team can have many players. The information consists
in team is the id as well the team name. However a
player can only play one team at any one time. These
players can play for many matches. Provide the
players details such as their id, name and position id
(every position belongs to a player).

✓ As the first step in the design process, identify the


objects and their attributes for the system. (DO NOT
write a class for this problem)
CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL
OOP CHARACTERISTICS

ABSTRACTION ENCAPSULATION
/DATA HIDING

POLYMORPHISM

INHERITANCE
OOP CHARACTERISTICS
ABSTRACTION

Definition
Process of taking away or removing characteristics from an
object in order to reduce it to a set of essential characteristics
(attributes and behavior of an object).

▪ Abstraction in Java allows the user to hide non essential details


relevant to user in order to reduce complexity and increase
efficiency
✓ It allows only to show the essential features of the object to the
end user.
✓ A user’s code should not access the implementation details of
the class used.
✓ In other sense we can say it deals with the outside view of an
object (interface).
OOP CHARACTERISTICS
ABSTRACTION

▪ The result of doing abstraction can also be called an


abstraction meaning a named entity made up of selected
attributes and behavior specific to an object.

▪ See http://whatis.techtarget.com/definition/abstraction
OOP CHARACTERISTICS
ENCAPSULATION (DATA HIDING)

Definition

Binding or wrapping‐up of data and associated methods into


a single unit (class)

▪ The data are not accessible to the outside world and only
those methods, which are wrapped in the class, can access
it – provide privacy to data and some of the methods

▪ Also known as data hiding.


OOP CHARACTERISTICS
INHERITANCE

Definition
Inheritance is a mechanism in OOP to design two or more
entities that are different but share many common features.

▪ The capability of a class to use the properties and methods of


another class while adding its own functionality.
– The new class will have the features of the old class.
- “is-a” relationship
- Concept of reusability
▪ Features common to all classes are defined in the superclass
(ancestor).
▪ The classes that inherit common features from the superclass
are called subclasses (descendant).
OOP CHARACTERISTICS
INHERITANCE (EXAMPLE)

Here are the superclass Account and its subclasses Savings and
Current.

Account

Current Savings
OOP CHARACTERISTICS
INHERITANCE (EXAMPLE)

An example of inheritance hierarchy among different types of


students.

Student

Graduate Undergrad

Masters Doctoral Law Commuting Resident


OOP CHARACTERISTICS
POLYMORPHISM

Definition
The ability of an object to take on many forms
✓ the capability of an action or method to do different things
based on the object that it is acting upon
✓ Allows programmer to send same message to objects from
different classes.
✓ Allows a single variable to refer objects from different
objects from different subclasses in the same inheritance
hierarchy.
OOP CHARACTERISTICS
POLYMORPHISM (EXAMPLE 1)

Example:

Given a superclass shape, polymorphism enables the user


to define different area methods for any number of subclasses,
such as circles, rectangles and triangles. No matter what
shape an object is, applying the area method to it will return
the correct results.
OOP CHARACTERISTICS
POLYMORPHISM (EXAMPLE 1)

SHAPES & ITS SUBCLASSES


OOP CHARACTERISTICS
POLYMORPHISM (EXAMPLE 2)

MONSTER & ITS SUBCLASSES


OOP CHARACTERISTICS
POLYMORPHISM (CODING EXAMPLE )

class Animal
{ public void animalSound()
{ System.out.println("The animal makes a sound"); }
}

class Cat extends Animal


{ public void animalSound()
{ System.out.println("The cat says: meow meow"); }
}

class Dog extends Animal


{ public void animalSound()
{ System.out.println("The dog says: bow wow"); }
}

Edited fromhttps://www.w3schools.com/java/java_polymorphism.asp
THANK YOU

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL 34

You might also like