oop lecture 1_v_2
TRANSCRIPT
MAKING COMPONENTS
Learning OutcomesAt the end of this topic, the student is able to :-i. Describe principle of object orientedii. Tells the difference between objects and classesiii. Reusing classes through inheritance and polymorphism
>>
Lectu
re 1
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
I. OBJECT ORIENTED PRINCIPLES
BEFORE WE STARTPre Requisite
>>
Lectu
re 1
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
2
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
>>
Lectu
re 1
3BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
PILLARS OF OBJECT ORIENTED
Lesson 1
>>
Lesso
n 1
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
4
Principles of Object Oriented
• Identify classes, object and relationships
1. Abstraction
• Hiding information
2. Encapsulation
• Extending class definition
3. Inheritance
• Using the same name with different behavior
4. Polymorphism
>>
Lesso
n 1
5BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
REPRESENTING OBJECTLesson 2
>>
Lesso
n 2
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
6
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.
>>
Lesso
n 2
7BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
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
>>
Lesso
n 2
8BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Can You Identify Objects?>
> Le
sson 2
9BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
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?
>>
Lesso
n 2
10
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Object Representation in UML>
> Le
sson 2
11
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Alf Green Turtle
4 legsHard shell50 years old54 degree 33’ North
SwimTalkFlapping legs
Identity
State
Behavior
Objects in Nemo’s World>
> Le
sson 2
12
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Alf Green Turtle4 legsHard shell50 years old54 degree 33’ NorthSwimTalkFlapping legs
Blue Dotted StingrayTwo eyesThin flexible tailSwimTalkMoving tail
Sydney BridgeMetalish Grey4 pillarsHalf-moon archChange colorResize
Young Green Turtle4 legsHard shell6 years oldSwimTalkFlapping legs
Simon Angle FishStripes Blue Yellow2 whiskersSwimTalkMoving tail
Types of Object
Tangible• Physical existence can
be felt or seen• Example
Intangible• Physical existence
cannot be felt or seen • Example
Conceptual• Derived from tangible
object• Example
>>
Lesso
n 2
13
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Pull down force Box AMass 20kgAcceleration 60mphCalculate force
Thinking JavaBruce Eckel300 pagesISBN 787887547INR 6000OpenCloseDisplayDiscount
MyQueue7 elementsFirst : 16Last : 20PushPopView
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.
>>
Lesso
n 2
14
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Defining Class>
> Le
sson 2
15
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Fish
ColorLengthNamePosition
Swim()Talk()MoveEyes()MoveLips()
Many objects
One class
Can You Identify Classes?>
> Le
sson 2
16
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Class Representation in UML>
> Le
sson 2
17
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Fish
ColorLengthNamePosition
Swim()Talk()MoveEyes()MoveLips()
Class name
Instance variables
Methods
Compartment for instance variables and methods can be hide
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?
>>
Lesso
n 2
18
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
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.
>>
Lesso
n 2
19
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
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
>>
Lesso
n 2
20
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
HIDING DETAILSLesson 3
>>
Lesso
n 3
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
21
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.
>>
Lesso
n 3
22
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
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.
>>
Lesso
n 3
23
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
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.
>>
Lesso
n 3
24
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Access Rights
Public Protected
Package Private
>>
Lesso
n 3
25
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
UML Notation for Access Rights
Public +
Protected #
Package ~
Private -
>>
Lesso
n 3
26
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Position of Access Rights>
> Le
sson 3
27
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
public
protected
package
private
Accessible to the world
Not accessible to the world
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.
>>
Lesso
n 3
28
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Accessing Public Members
• 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
>>
Lesso
n 3
29
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Fish
+Color+Length+Name+Position
+Swim()+Talk()+MoveEyes()+MoveLips()
Aquarium
LengthWidthHeightWater
FillWater()FIllOxygen()AddFish()
Accessing Public Members
• The Aquarium can make the Fish swim() – because it is public– We make the fish swims in the aquarium
>>
Lesso
n 3
30
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Fish
+Color+Length+Name+Position
+Swim()+Talk()+MoveEyes()+MoveLips()
Aquarium
LengthWidthHeightWater
FillWater()FIllOxygen()AddFish()
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.
>>
Lesso
n 3
31
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Accessing Private Members
• The Aquarium cannot make the Fish swim() – because it is private
>>
Lesso
n 3
32
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Fish
-Color-Length-Name-Position
-Swim()-Talk()+MoveEyes()+MoveLips()
Aquarium
LengthWidthHeightWater
FillWater()FIllOxygen()AddFish()
X
X
Accessing Private Members
• Private members are accessible within internal class members only
>>
Lesso
n 3
33
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Fish
-Color-Length-Name-Position
-Swim()-Talk()+MoveEyes()+MoveLips()
Aquarium
LengthWidthHeightWater
FillWater()FIllOxygen()AddFish()
Other Access Rights
• Protected and package will be addressed in later part of this subject
>>
Lesso
n 3
34
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
INHERITANCELesson 4
>>
Lesso
n 4
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
35
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.
>>
Lesso
n 4
36
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
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.
>>
Lesso
n 4
37
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Designing Inheritance
• Shark inherits from Fish– Shark is-a Fish (Term of relationship for inheritance in
OO model)
>>
Lesso
n 4
38
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Fish
-Color-Length-Name-Position
-Swim()-Talk()+MoveEyes()+MoveLips()
Shark
-SharpTeeth
+LaughLikeEvil()
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.
>>
Lesso
n 4
39
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Designing Inheritance>
> Le
sson 4
40
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Fish
-Color-Length-Name-Position
-Swim()-Talk()+MoveEyes()+MoveLips()
Shark
-SharpTeeth
+LaughLikeEvil()
Superclass
Subclass
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.
>>
Lesso
n 4
41
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Inheritance
• The Shark can Swim(), Talk(), MoveEyes() and MoveLips().• However, the Fish cannot LaughLikeEvil() and do not have
SharpTeeth
>>
Lesso
n 4
42
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Fish
-Color-Length-Name-Position
-Swim()-Talk()+MoveEyes()+MoveLips()
Shark
-SharpTeeth
+LaughLikeEvil()
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.
>>
Lesso
n 4
43
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Degree of Inheritance
• Inheritance comes in two fashion– Multiple inheritance
– Multilevel inheritance
>>
Lesso
n 4
44
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Multiple Inheritance
• A subclass is having multiple inheritance when it has two or more superclass.
• The subclass inheriting all features from multiple super classes.
>>
Lesso
n 4
45
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Designing Multiple Inheritance
• The Aeroplane inheriting the features from the FlyingObject and the Transportation.– It makes the Aeroplane can fly and used by human to
>>
Lesso
n 4
46
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
FlyingObject Transportation
Aeroplane
Multilevel Inheritance
• Mobilephone inherits features from Telephone.
• Smartphone inherits features from Mobilephone.
*** Thus, Smartphone is also inheriting features from Telephone ***
>>
Lesso
n 4
47
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Telephone
Mobilephone
Smartphone
SAME DIFFERENTLesson 5
>>
Lesso
n 5
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
48
How Does The Object Moves?>
> Le
sson 5
49
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
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
>>
Lesso
n 5
50
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
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
>>
Lesso
n 5
51
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
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
>>
Lesso
n 5
52
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Objects Behavior
• Objects acts and reacts in the environment– This is known as object behavior
• In programming, object behavior is translated into method.
>>
Lesso
n 5
53
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
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.
>>
Lesso
n 5
54
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Method Signature
• Note: Parameter consist of data type and name of parameter
>>
Lesso
n 5
55
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
public boolean swim (double angle, String direction) {
// insert method implementation here
return true;}
1 23 4
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.
>>
Lesso
n 5
56
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Shark inherits from Fish>
> Le
sson 5
57
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Fish
-Color-Length-Name-Position
+Swim()+Talk()+MoveEyes()+MoveLips()
Shark
-SharpTeeth
+LaughLikeEvil()+ Swim()
Overridden method
Implementation of Method in Fish>
> Le
sson 5
58
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Fish
-Color-Length-Name-Position
+Swim()+Talk()+MoveEyes()+MoveLips()
/* The following method display angle and direction */
public boolean swim (double angle, String direction) {
System.out.print(“Move to “ + direction); System.out.println(“with angle “, angle);
return true;}
Implementing Method Overriding>
> Le
sson 5
59
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
/* This method change the direction of the angle by 30 degree and change the direction value */
public boolean swim (double angle, String direction) {
if (angle <= 90) angle += 90;
else angle -= 90;
if (direction.equals(“West”)direction = “East”;
System.out.println(“New direction = “ + direction); System.out.println(“New angle = “ + angle); return true;}
Shark
-SharpTeeth
+LaughLikeEvil()+ Swim()
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.
>>
Lesso
n 5
60
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Implementing Method Overloading>
> Le
sson 5
61
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
Shark
-SharpTeeth
+LaughLikeEvil()+ Swim()+ ChangeColor(Color:String)+ChangeColor(Red:int, Blue: int, Green:int)
Overloaded methods
Implementing Method Overloading>
> Le
sson 5
62
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
/* 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;}
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
>>
Lesso
n 5
63
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
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.
>>
Lesso
n 5
64
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
What Have You Learn Today?
Learning Outcomes Check Yourself Checked
i. Describe principles of object oriented
i. What are the principles of OO?
ii. Tells the difference between objects and classes.
i. What are the characteristics of objects?
ii. What are the characteristics of class?
iii. Give examples of objects and classes.
iii. Reusing classes through inheritance and polymorhphism
i. How does inheritance and polymorphism provides higher degree of reusability?
>>
Lectu
re 1
65
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
THAT’S WRAP FOR TODAYSee You in the Next Class
>>
Lectu
re 2
BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri
66