Topic 1-3 Objects & Classes
Topic 1-3 Objects & Classes
TYPE EXAMPLE
Tangible (can be seen & touched) Vehicle, Employee, Clock
Intangible (conceptual) Account, Loan, Time, Date
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.
For the video object, the state might be rented, available and current
number of stock
INTRODUCTION TO OBJECTS
ELEMENTS OF AN OBJECT (Try this out ☺)
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
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
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.
▪ You can send a message only to the classes and objects that
understand the message you sent to them.
verify
request withdrawal
dispense money
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).
▪ See http://whatis.techtarget.com/definition/abstraction
OOP CHARACTERISTICS
ENCAPSULATION (DATA HIDING)
Definition
▪ 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
Definition
Inheritance is a mechanism in OOP to design two or more
entities that are different but share many common features.
Here are the superclass Account and its subclasses Savings and
Current.
Account
Current Savings
OOP CHARACTERISTICS
INHERITANCE (EXAMPLE)
Student
Graduate Undergrad
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: