1. >> Lecture 1
MAKING COMPONENTS I. OBJECT ORIENTED PRINCIPLES
Learning Outcomes
At the end of this topic, the student is able to :-
i. Describe principle of object oriented
ii. Tells the difference between objects and classes
iii. Reusing classes through inheritance and polymorphism
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
2. >> Lecture 1
Pre Requisite
BEFORE WE START
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
2
3. >> Lecture 1
Pre Requisite of this Chapter
• You should know what is entity
• You should know how to define and write functions
• You should know how function works
BITP 3113 Object Oriented Programming
Sem I 2012/2013
3
Auhtored by Emma McKay-Fikri
4. >> Lesson 1
Lesson 1
PILLARS OF OBJECT ORIENTED
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
4
5. >> Lesson 1
Principles of Object Oriented
1. Abstraction
• Identify classes, object and relationships
2. Encapsulation
• Hiding information
3. Inheritance
• Extending class definition
4. Polymorphism
• Using the same name with different behavior
BITP 3113 Object Oriented Programming
Sem I 2012/2013
5
Auhtored by Emma McKay-Fikri
6. >> Lesson 2
Lesson 2
REPRESENTING OBJECT
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
6
7. >> Lesson 2
1. Abstraction
• Abstraction is an act to represent real world object
into software component.
• It is a process to identify objects and classes for the
system.
BITP 3113 Object Oriented Programming
Sem I 2012/2013
7
Auhtored by Emma McKay-Fikri
8. >> Lesson 2
Object
• Object is the main component in object oriented
programming.
• An object can be considered as a “thing” that will
do a set of related activities
BITP 3113 Object Oriented Programming
Sem I 2012/2013
8
Auhtored by Emma McKay-Fikri
9. >> Lesson 2
Can You Identify Objects?
BITP 3113 Object Oriented Programming
Sem I 2012/2013
9
Auhtored by Emma McKay-Fikri
10. >> Lesson 2
How to Identify Object?
• Object has Identity
– What makes an object different from another object?
• Object has State/ Characteristic
– What is the data of the object?
• Object has Behavior
– What the object can do?
– What can we do with the object?
BITP 3113 Object Oriented Programming 1
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
0
11. >> Lesson 2
Object Representation in UML
Alf Green Turtle Identity
4 legs
Hard shell
50 years old State
54 degree 33’ North
Swim
Talk Behavior
Flapping legs
BITP 3113 Object Oriented Programming 1
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
1
12. >> Lesson 2
Objects in Nemo’s World
Sydney Bridge
Metalish Grey
4 pillars
Young Green Turtle Half-moon arch
4 legs Change color
Hard shell Resize
6 years old
Swim
Talk
Flapping legs
Simon Angle Fish
Stripes Blue Yellow
2 whiskers
Swim
Talk
Moving tail
Alf Green Turtle
4 legs Blue Dotted Stingray
Hard shell Two eyes
50 years old Thin flexible tail
54 degree 33’ North Swim
Swim Talk
Talk Moving tail
Flapping legs
BITP 3113 Object Oriented Programming 1
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
2
13. >> Lesson 2
Types of Object
Tangible Intangible Conceptual
•Physical existence can •Physical existence •Derived from tangible
be felt or seen cannot be felt or seen object
•Example •Example •Example
Thinking Java
Bruce Eckel Pull down force Box A
300 pages Mass 20kg MyQueue
ISBN 787887547 Acceleration 60mph 7 elements
INR 6000 Calculate force First : 16
Open
Last : 20
Close
Push
Display
Pop
Discount
View
BITP 3113 Object Oriented Programming 1
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
3
14. >> Lesson 2
Defining a Class
• In object oriented programming,
– Objects are created
– Classes are defined
• Object is an instance of a class
• A class is a general definition for a group of objects
that has similar characteristics and behavior.
BITP 3113 Object Oriented Programming 1
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
4
15. >> Lesson 2
Defining Class
Many objects
Fish
Color
Length
Name
Position
One class
Swim()
Talk()
MoveEyes()
MoveLips()
BITP 3113 Object Oriented Programming 1
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
5
16. >> Lesson 2
Can You Identify Classes?
BITP 3113 Object Oriented Programming 1
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
6
17. >> Lesson 2
Class Representation in UML
Fish Class name
Color
Length
Name Instance variables
Position
Swim()
Talk() Methods
MoveEyes()
MoveLips()
Compartment for instance variables and methods can be hide
BITP 3113 Object Oriented Programming 1
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
7
18. >> Lesson 2
Designing a Class
• A class must have a Name
– What is the unique name for a group of object?
• A class should have Instance variables
– What the class should know?
– What is the data or attribute of the class?
• A class should have Method
– How to class act and react?
– What is the name of the action and reaction?
BITP 3113 Object Oriented Programming 1
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
8
19. >> Lesson 2
It Comes in a Package!
• If you notice, instance variables and methods are
packaged together in a class.
• This is because it is not logical to separate
data/information and method/functions from a
class.
– Because in real world, data and functions are not
separated into different packages.
• The class model must reflects the real world entity.
BITP 3113 Object Oriented Programming 1
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
9
20. >> Lesson 2
Real World is Complex
• Real world object vary in complexity
• Some objects has similar behavior but not the same
– Lamp (on, off, turnOn, turnOff)
– Radio (on, off, frequency, volume, turnOn, turnOff, tuning, increaseVolume,
decreaseVolume)
• Some objects are extended from another object
– Student (name, cgpa, takeExam())
– PostGraduate (researchArea, supervisor, presentProgress())
• PostGraduate is an extended class from student
• Some objects contains another objects
– Car (name, model, door, color, engine)
– Engine (model, capacity)
• Engine is an object resides in a car
BITP 3113 Object Oriented Programming 2
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
0
21. >> Lesson 3
Lesson 3
HIDING DETAILS
BITP 3113 Object Oriented Programming
Sem I 2012/2013
2
Auhtored by Emma McKay-Fikri 1
22. >> Lesson 3
Objects Communication
• Object does not work in isolation.
• One object will be communicating with other
objects to make the application works.
• Object communicates by exchanging messages.
– That is invoking behavior (or methods in a class)
• Since other objects can have an access to data
and/or method in other objects, we need to have
some kind of protection for a sensitive data.
BITP 3113 Object Oriented Programming 2
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
2
23. >> Lesson 3
2. Encapsulation
• Encapsulation is hiding information from the world.
• The world do not need to know the details
of the class and how it works.
• What the world need to know is what the class
offers to the world to make the application works.
BITP 3113 Object Oriented Programming 2
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
3
24. >> Lesson 3
Designing Encapsulation
• Encapsulation limits external class access to a
particular class
• This can be achieve by defining the access right to
instance variables and methods.
• Access right is a grant given to external class
having an access to instance variables and
method.
BITP 3113 Object Oriented Programming 2
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
4
25. >> Lesson 3
Access Rights
Public Protected
Package Private
BITP 3113 Object Oriented Programming 2
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
5
26. >> Lesson 3
UML Notation for Access Rights
Public +
Protected #
Package ~
Private -
BITP 3113 Object Oriented Programming 2
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
6
27. >> Lesson 3
Position of Access Rights
Accessible to
the world
public
protected
package
Not accessible to
the world
private
BITP 3113 Object Oriented Programming 2
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
7
28. >> Lesson 3
Implication of Public Access Rights
• Public access rights means the world can have
direct access to the instance variables
and/method.
• Public instance variables allow the external class
to access and modify the value.
– This can be very dangerous. Sensitive data must have
some kind of protection.
• Public methods allow the external class to invoke
them.
BITP 3113 Object Oriented Programming 2
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
8
29. >> Lesson 3
Accessing Public Members
Fish Aquarium
+Color Length
+Length Width
+Name Height
+Position Water
+Swim() FillWater()
+Talk() FIllOxygen()
+MoveEyes() AddFish()
+MoveLips()
• The Aquarium can change the Color of the Fish
– This is so wrong in real world. We can’t change the color of the
fish!!!
– We need to protect the Color of the Fish
BITP 3113 Object Oriented Programming 2
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
9
30. >> Lesson 3
Accessing Public Members
Fish Aquarium
+Color Length
+Length Width
+Name Height
+Position Water
+Swim() FillWater()
+Talk() FIllOxygen()
+MoveEyes() AddFish()
+MoveLips()
• The Aquarium can make the Fish swim() – because it is public
– We make the fish swims in the aquarium
BITP 3113 Object Oriented Programming 3
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
0
31. >> Lesson 3
Implication of Private Access Rights
• Private access right means the world do not
have an access to the instance variables
and/or methods.
• Private instance variables and methods are
accessible to other members of the class.
• Normally, we protect sensitive data (instance
variable) using private access rights.
BITP 3113 Object Oriented Programming 3
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
1
32. >> Lesson 3
Accessing Private Members
Fish Aquarium
-Color Length
-Length
-Name X Width
Height
Water
-Position
-Swim()
-Talk()
X FillWater()
FIllOxygen()
+MoveEyes() AddFish()
+MoveLips()
• The Aquarium cannot make the Fish swim() – because it is
private
BITP 3113 Object Oriented Programming 3
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
2
33. >> Lesson 3
Accessing Private Members
Fish Aquarium
-Color Length
-Length Width
-Name Height
-Position Water
-Swim() FillWater()
-Talk() FIllOxygen()
+MoveEyes() AddFish()
+MoveLips()
• Private members are accessible within internal class
members only
BITP 3113 Object Oriented Programming 3
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
3
34. >> Lesson 3
Other Access Rights
• Protected and package will be addressed in later
part of this subject
BITP 3113 Object Oriented Programming 3
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
4
35. >> Lesson 4
Lesson 4
INHERITANCE
BITP 3113 Object Oriented Programming
Sem I 2012/2013
3
Auhtored by Emma McKay-Fikri 5
36. >> Lesson 4
3. Extending Class Definition
• Object oriented promotes higher reusability
• This can be achieved through inheritance
• Inheritance is extending a class definition
using existing class.
– No re-definition (re-writing) is required
• The new class may have additional instance
variables and/or methods.
BITP 3113 Object Oriented Programming 3
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
6
37. >> Lesson 4
Designing Inheritance
• Inheritance is design in hierarchical form which
users subclass and superclass.
• Superclass is more general and can be the existing
class.
• Subclass is a specialized class with additional
instance variables and/or methods.
• Superclass can be derived when common
instance variables and methods exist among
classes.
BITP 3113 Object Oriented Programming 3
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
7
38. >> Lesson 4
Designing Inheritance
Fish
-Color
-Length Shark
-Name
-Position -SharpTeeth
-Swim() +LaughLikeEvil()
-Talk()
+MoveEyes()
+MoveLips()
• Shark inherits from Fish
– Shark is-a Fish (Term of relationship for inheritance in OO
model)
BITP 3113 Object Oriented Programming 3
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
8
39. >> Lesson 4
Relating Subclass and Superclass
• In UML, relating subclass and superclass is using
solid arrow head.
• The arrow will be pointing to the superclass.
• The subclass will be at the other end.
BITP 3113 Object Oriented Programming 3
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
9
40. >> Lesson 4
Designing Inheritance
Fish
-Color
-Length Shark
-Name
-Position -SharpTeeth
-Swim() +LaughLikeEvil()
-Talk()
Subclass
+MoveEyes()
+MoveLips()
Superclass
BITP 3113 Object Oriented Programming 4
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
0
41. >> Lesson 4
Inheritance
• The subclass inherits all instance variables and/or
methods from superclass.
– Applies to non private only
– What the superclass can do, the subclass can do as well
• This means, subclass can have a direct access to
all instance variables and/or methods which are
not private.
• However, superclass do not have an access to any
members in subclass.
BITP 3113 Object Oriented Programming 4
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
1
42. >> Lesson 4
Inheritance
Fish
-Color
-Length Shark
-Name
-Position -SharpTeeth
-Swim() +LaughLikeEvil()
-Talk()
+MoveEyes()
+MoveLips()
• The Shark can Swim(), Talk(), MoveEyes() and MoveLips().
• However, the Fish cannot LaughLikeEvil() and do not have
SharpTeeth
BITP 3113 Object Oriented Programming 4
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
2
43. >> Lesson 4
Not a Camouflaging
• A subclass object can be a super class object
– Because a sub class inherits (almost all) properties /
member from super class
• However, a superclass cannot be a subclass
– Because a superclass is not inheriting and having an
access to sub class properties/member.
BITP 3113 Object Oriented Programming 4
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
3
44. >> Lesson 4
Degree of Inheritance
• Inheritance comes in two fashion
– Multiple inheritance
– Multilevel inheritance
BITP 3113 Object Oriented Programming 4
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
4
45. >> Lesson 4
Multiple Inheritance
• A subclass is having multiple inheritance
when it has two or more superclass.
• The subclass inheriting all features from multiple
super classes.
BITP 3113 Object Oriented Programming 4
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
5
46. >> Lesson 4
Designing Multiple Inheritance
FlyingObject Transportation
Aeroplane
• The Aeroplane inheriting the features from the FlyingObject
and the Transportation.
– It makes the Aeroplane can fly and used by human to
BITP 3113 Object Oriented Programming 4
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
6
47. >> Lesson 4
Multilevel Inheritance
Telephone
• Mobilephone inherits features
Mobilephone
from Telephone.
• Smartphone inherits features
from Mobilephone.
Smartphone
*** Thus, Smartphone is also
inheriting features from Telephone
***
BITP 3113 Object Oriented Programming 4
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
7
48. >> Lesson 5
Lesson 5
SAME DIFFERENT
BITP 3113 Object Oriented Programming
Sem I 2012/2013
4
Auhtored by Emma McKay-Fikri 8
49. >> Lesson 5
How Does The Object Moves?
BITP 3113 Object Oriented Programming 4
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
9
50. >> Lesson 5
Object Moves
• Turtle – flapping their legs
• Fish – swims (moving the body, tail, fin)
• Seahorse – using its tiny fin?
• Blowfish – floating
• Jellyfish – moving the legs
• Bird – paddling using legs, flying
BITP 3113 Object Oriented Programming 5
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
0
51. >> Lesson 5
4. Polymorphism
• Notice that all the objects can move
• However, the way it moves are different
• This is known as polymorphism
– Same name but different mechanism
BITP 3113 Object Oriented Programming 5
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
1
52. >> Lesson 5
Implementing Polymorphism
• In programming, polymorphism is implemented
using two approaches.
– Method overriding
– Method/constructor overloading
• Note : Details of constructor will be covered in the later part of this subject
BITP 3113 Object Oriented Programming 5
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
2
53. >> Lesson 5
Objects Behavior
• Objects acts and reacts in the environment
– This is known as object behavior
• In programming, object behavior is translated into
method.
BITP 3113 Object Oriented Programming 5
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
3
54. >> Lesson 5
Method Signature
• Objects communicate through message passing.
• Message that is to be pass contains the name of
the message and data.
• Message name and data is translated into
programming and known as method signature.
• Method signature is referring to the 1. method
name, 2. list of parameters, 3. access right and 4.
return type.
BITP 3113 Object Oriented Programming 5
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
4
55. >> Lesson 5
Method Signature
3 4 1 2
public boolean swim (double angle, String direction) {
// insert method implementation here
return true;
}
• Note: Parameter consist of data type and name of parameter
BITP 3113 Object Oriented Programming 5
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
5
56. >> Lesson 5
Method Overriding
• This approach is achievable through inheritance
only.
• Method overriding is method with the same
name and signature but different implementation.
– Access right could be different
• Subclass is overriding method define in super class.
BITP 3113 Object Oriented Programming 5
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
6
57. >> Lesson 5
Shark inherits from Fish
Fish
-Color
-Length Shark
-Name
-Position -SharpTeeth
+Swim() +LaughLikeEvil()
+Talk() + Swim()
+MoveEyes()
+MoveLips()
Overridden
method
BITP 3113 Object Oriented Programming 5
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
7
58. >> Lesson 5
Implementation of Method in Fish
Fish
-Color
/* The following method display angle and direction */
-Length
-Name public boolean swim (double angle, String direction) {
-Position
System.out.print(“Move to “ + direction);
System.out.println(“with angle “, angle);
+Swim()
+Talk() return true;
+MoveEyes() }
+MoveLips()
BITP 3113 Object Oriented Programming 5
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
8
59. >> Lesson 5
Implementing Method Overriding
/* This method change the direction of the angle by
30 degree and change the direction value */
public boolean swim (double angle, String direction) {
Shark
if (angle <= 90)
angle += 90;
-SharpTeeth
else
angle -= 90;
+LaughLikeEvil()
+ Swim() if (direction.equals(“West”)
direction = “East”;
System.out.println(“New direction = “ + direction);
System.out.println(“New angle = “ + angle);
return true;
}
BITP 3113 Object Oriented Programming 5
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
9
60. >> Lesson 5
Method Overloading
• Method overloading is methods that have the
same name with different signature and different
implementation.
– Return type could be different.
• Overloaded methods are defined in the same
class.
BITP 3113 Object Oriented Programming 6
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
0
61. >> Lesson 5
Implementing Method Overloading
Shark
-SharpTeeth
+LaughLikeEvil()
+ Swim()
Overloaded + ChangeColor(Color:String)
methods +ChangeColor(Red:int, Blue: int, Green:int)
BITP 3113 Object Oriented Programming 6
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
1
62. >> Lesson 5
Implementing Method Overloading
/* This method change the Color of the shark */
public void changeColor(String color) {
this.color = color;
}
/* This method change the Color of the shark using
RGB value */
public void changeColor(int red, int blue, int green) {
this.color.RED = red;
this.color.BLUE = blue;
this.color.GREEN = green;
}
BITP 3113 Object Oriented Programming 6
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
2
63. >> Lesson 5
No Re-Writing
• Inheritance and polymorphism provides higher
degree of reusability.
• An advanced definition of a class (from existing)
requires no re definition from base instance
variables or methods (no re-writing).
• The new class can be extended from existing
class.
– Less hassle, maintainable and stable
BITP 3113 Object Oriented Programming 6
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
3
64. >> Lesson 5
Common Understanding
• Method overloading enriched the model, which
the same method is able to process different kinds
of input data.
• This has increase the reusability of the design using
common name.
BITP 3113 Object Oriented Programming 6
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
4
65. >> Lecture 1
What Have You Learn Today?
Learning Outcomes Check Yourself Checked
i. Describe principles i. What are the principles of
of object oriented OO?
ii. Tells the difference i. What are the characteristics
between objects of objects?
and classes. ii. What are the characteristics
of class?
iii. Give examples of objects
and classes.
iii. Reusing classes i. How does inheritance and
through inheritance polymorphism provides
and polymorhphism higher degree of reusability?
BITP 3113 Object Oriented Programming 6
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
5
66. >> Lecture 2
See You in the Next Class
THAT’S WRAP FOR TODAY
BITP 3113 Object Oriented Programming
Sem I 2012/2013
6
Auhtored by Emma McKay-Fikri 6