Lecture 1
Lecture 1
Lecture 1
Review of OOP
Outline
• OOP and Procedural Programming
• Encapsulation
• Inheritance
• Polymorphism
2
References
3
OOP and Procedural Programming
4
OOP and Procedural Programming
5
OOP and Procedural Programming
6
OOP and Procedural Programming
playSound(shapeNum) {
// if the shape is not an amoeba
// use shapeNum to lookup which
// Flac sound to play, and play it
// else
// play amoeba .mp3 sound}
Touch previously tested code so need to change Don’t have to touch code he’d already tested and delivered. Only test
new added code.
code test and test all code again.
7
OOP and Procedural Programming
8
OOP and Procedural Programming
Add rotation point arguments to the rotate procedure. modified the rotate method, but only in the Amoeba class.
A lot of code was affected. So need to test all code again. Don’t touch the tested, working, compiled code for the other parts of
the program. Only test new added code.
9
OOP and Procedural Programming
Benefit:
• OOP are more secure in data.
• OOP can maintain code more easily.
• OOP can extend code more easily.
• OOP increases reuse of code by applying inheritance and polymorphism.
10
Encapsulation
11
Get, Set method and Encapsulation
• Imagine you wrote the code for a class and another dozen programmers
from your company all wrote programs that used your class.
P1 code P2 code
12
Encapsulation
Other programmers that use P1 code Other programmers that use P2 code
13
Encapsulation
P1 code P2 code
14
Encapsulation
15
Inheritance
16
Inheritance
17
Inheritance
18
Problem with the design
19
Inheritance
• Pull out the common properties into a new position (a new class, called
parent class or superclass).
20
Inheritance
Benefits
Extensibility
Eliminate duplicates
21
Inheritance
22
Type hierarchy :
List
23
Type hierarchy :
Exceptions
24
IS-A and Has-A Relationships
Code Explanation
25
Polymorphism
26
Polymorphism
27
Reference variable
28
Substitution Principle
Code Explanation
29
Substitution Principle
30
Signature Rule
The subtype objects must have all the methods of the supertype, and the
signatures of the subtype methods(Overriding) must be compatible with
the signatures of the corresponding supertype methods (Overridden
methods).
• Signature compatition:
• Return type: same type or one of the subtypes
• Exception: a subtype method can have fewer exceptions than the
corresponding supertype method
• The signature rule guarantees that every call that is type correct
according to the supertype’s definition is also type correct for the
subtype. This requirement is enforced by the Java compiler.
31
Methods rule
A subtype method can weaken the precondition and can strengthen the
postcondition. The precondition is what must be guaranteed to hold by
the caller in order to make the call. The postcondition is what is
guaranteed to hold right after the call (assuming that the precondition
held when the call was made).
Code Explanation
32
Properties rule
• The subtype must preserve all properties that can be proved about
supertype objects.
• Example: A supertype named Parent and an attribute B. ( B > 10)
A subtype of Parent named Child. (B > 5 & B < 20)
The following is the correct restriction on B in a subtype:
33
Java’s dynamic method dispatching
34
Benefits of Polymorphism
35
Summary
36