ay17/18 sem 2cs2030/1718-s2/cs2030-lec03.pdf · ay17/18 sem 2 object reference instance fields...
TRANSCRIPT
AY17/18 Sem 2
AY17/18 Sem 2
Lecture 3Inheritance (Continued)
AY17/18 Sem 2
Lab starts tomorrow
Lab• Lab 0: ungraded
• Submit anyway, by Friday midnight
• BYOD: Bring your own device
• Get ready:
• install Java 9
• familiarize with UNIX
AY17/18 Sem 2
Last week, in cs2030..
AY17/18 Sem 2
Java uses late binding for methods
invocation
AY17/18 Sem 2
object reference
instance fields
pointer to method table
pointer to class fields
pointer to print()
{ : }
Circle
object reference
pointer to method table
instance fields
{ : }
Point
pointer to class fields
pointer to print()
object reference
objectsToPrint[ ]
0
1
2
pointer to method table
instance fields
:
:
AY17/18 Sem 2
Polymorphism
AY17/18 Sem 2
“Each significant piece of functionality in a program should be implemented in just one place in the source code. “
AY17/18 Sem 2
PaintedShape
PaintedCircle
- fillWith- setBorderColor- setBorderThickness
- getArea- getPerimeter- contains
PaintedSquare
- getArea- getPerimeter- contains
AY17/18 Sem 2
Inheritance
AY17/18 Sem 2
Circle
- getArea- getPerimeter- contains- equals
Object- equals- toString- …
AY17/18 Sem 2
object reference
c: (0, 0) r: 10
pointer to method table
pointer to class fields
pointer to print()
{ : }
Circle
object reference
pointer to method table
c: (0, 0) r: 10
object reference
circles[ ]
0
1
2
:
AY17/18 Sem 2
@Overridepublic boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof Circle) { Circle circle = (Circle) obj; return (circle.center.equals(center) && circle.radius == radius); } else return false;}
AY17/18 Sem 2
c
c: (0, 0) r: 10
pointer to method table
getArea(){ : }
Circle
:
getPerimeter()
{ : }
toString( )
equals(Object)
:{ : }
{ : }
Object
Object
AY17/18 Sem 2
c
c: (0, 0) r: 10
pointer to method table
getArea(){ : }
Circle
:
getPerimeter()
{ : }
toString( )
equals(Object)
:{ : }
{ : }
Object
Object
AY17/18 Sem 2
c
c: (0, 0) r: 10
pointer to method table
getArea(){ : }
Circle
:
getPerimeter()
{ : }
toString( )
equals(Object)
:{ : }
{ : }
Object
Object
{ : }
equals(Circle)
AY17/18 Sem 2
boolean equals(Circle c)
enough?
AY17/18 Sem 2
• To call equals(Circle c), the caller needs to know the existence of Circle
• Code that calls equals(Circle c) can’t be very general
AY17/18 Sem 2
• Everyone should call equals(Object c)
• Example: ArrayList’s method contains(Object o)
AY17/18 Sem 2
Circle
- getArea- getPerimeter- contains- equals
Object- equals- toString- …
AY17/18 Sem 2
We have the power to change how existing code behave
through overriding and polymorphism.
AY17/18 Sem 2
Abstraction Barrier between parent and
child
AY17/18 Sem 2
Abstraction Barrier
implementation of circle
usage of circleclient
implementer
AY17/18 Sem 2
Abstraction Barrierclass PaintedCircle extends Circle { :}
client
implementerclass Circle { : :}
class Point { : :}
AY17/18 Sem 2
object reference
instance fields
pointer to class info
pointer to class fields
method table
class fields
inherited method
method
method
object
class
inherited instance
fields
inherited method
Only non-private fields / methods are accessible by the subclass
AY17/18 Sem 2
Everyone Same class
public Yes Yes
private No Yes
AY17/18 Sem 2
Everyone Same class Subclass
public Yes Yes Yes
private No Yes No
protected No Yes Yes
AY17/18 Sem 2
Abstraction Barrier
implementation of a package (set of related
classes and interfaces)
usage of the packageclient
implementer
AY17/18 Sem 2
Everyone Same class Subclass Package
public Yes Yes Yes Yes
private No Yes No No
protected No Yes Yes Yes
No Yes No Yes
AY17/18 Sem 2
Inheritance (done right)
AY17/18 Sem 2
Point- double x, y
Object-
Circle- double radius
Cylinder- double height
AY17/18 Sem 2
cylinder.getPerimeter()circle.contains(cylinder)
it gets weird!
AY17/18 Sem 2
Point- double x, y- ..
Object-
Circle
- Point center- double radius- ..
Cylinder
- Circle base- double height- ..
AY17/18 Sem 2
A circle has a point as its center
A cylinder has a circle as its base
AY17/18 Sem 2
A circle is composed of a point as its center and a radius
A cylinder is composed of a circle as its base and a height
AY17/18 Sem 2
Composition
AY17/18 Sem 2
A PaintedShape is composed of style information and geometric information
AY17/18 Sem 2
Style- Color fillColor- ..
Object-
Shape- getArea()- getPerimeter()- ..
PaintedShape- Style style- Shape shape
AY17/18 Sem 2
Inheritance is better for “is a” relationship
AY17/18 Sem 2
Circle
- getArea()- getPerimeter()- contains()- equals()
PaintedCircle
- Style style
AY17/18 Sem 2
A painted circle is a type of circle
Everywhere where a circle is used, we can replace it with another circle (with style)
AY17/18 Sem 2
cylinder.setBase(circle)paintedCircle.contains(p)
not weird
AY17/18 Sem 2
A square is a rectangle
AY17/18 Sem 2
square.setSize(w, h)
it’s weird
AY17/18 Sem 2
void fillScreen(Rectangle r) { r.setSize(1280, 960);}
AY17/18 Sem 2
hey! who messes with my rectangle?
AY17/18 Sem 2
“if S is a subclass of T, then an object of type T can be replaced by an object of type S without changing the desirable property of the program “
Liskov Substitution Principle (LSP)
AY17/18 Sem 2
Barbara Liskov, MIT
AY17/18 Sem 2
We have the power to change how existing code behave
through overriding and polymorphism.
AY17/18 Sem 2
With great power comes great responsibility “ “
AY17/18 Sem 2
Uncle Ben
AY17/18 Sem 2
It is our responsibility to ensure that LSP is observed when we inherit a class, so that we don’t break other’s
people code
AY17/18 Sem 2
Preventing Inheritance and
overriding with final