Professional Documents
Culture Documents
R. Grin
Inheritance
page 2
Reusability
q If you get a compiled A a = new A(); int y = a.compute(x); q But, sometimes, you would like to modify slightly the behavior of the instances of A q It is not possible if you have not got the source code
Inheritance
can use it: q Wi t h inheritance you can create an object whose behavior is slightly different from an instance of the A class: an instance of a subclass of A, w i t h
n new
A.class you
methods A methods
n modified
A.java
R. Grin
page 3
R. Grin
page 4
Inheritance in Java
q Each class has one and only one superclass: no multiple i n h e r i t a n c e q The class inherits t h e members of its superclass q It is possible t o
n add
Inheritance in Java
q The keyword of a class:
extends indicates
the superclass
n modify
R. Grin
page 5
R. Grin
page 6
Example superclass
public class Rectangle { private int x, y; // upper left point private int width, height;
// Constructors // Methods getX(), setX( int), // getWidth(), getHeight (), // contains(Point), intersects(Rectangle), // translate(int, int), toString() . . .
Example - subclass
public class ColoredRectangle extends Rectangle { private Color color; // new variable // Constructors . . . // New methods
Examples
q Superclass q q q
Vehicle and subclasses Bicycle, Car and Truck Bicycle inherits from Vehicle and have several subclasses: MontainBike, RacingBike, Employe inherits from Person Image is the s u p e r c l a s s of GifImage et JpegImage
from
R. Grin
page 9
R. Grin
page 10
Overriding methods
R. Grin
page 11
R. Grin
page 12
super
manipulate a method of the superclass, overridden (or not) in the current class:
R. Grin
page 13
R. Grin
page 14
Constructor Chaining
q E xceptions : the overriding method cannot declare throwing more exceptions than the overridden method declares (subclasses are allowed)
R. Grin
page 15
R. Grin
page 16
Superclass
public class Rectangle { private int x, y, weight, height; public Rectangle(int x, int y, int weight, int height) { this.x = x; this.y = y; this.weight = weight; this.height = height; } . . . }
constructor of the s u p e r c l a s s:
super(...)
n another
this(...)
q Otherwise, the compiler adds an implicit call to the constructor without parameter of the superclass
R. Grin
page 17
R. Grin
page 18
public ColoredRectangle(int x, int y, int weight, int height, Color color) { super(x, y, weight, height); this.color = color; } public ColoredRectangle (int x, int y, int weight, int height) { this(x, y, weight, height, Color.black);
} . . . }
R. Grin Java: inheritance and polymorphism page 19
A constructor of the s u p e r c l a s s is a l w a y s
executed before the other instructions of the constructor
R. Grin
page 20
Protected access
R. Grin
page 21
R. Grin
page 22
What is inherited ?
q If B extends A, it inherits of all the members of A (but not the constructors) q But, B cannot access private members of the A class; these members will be implicitly used by B, but B will not be able to name or to manipulate them q
protected
protected
the classes of t h e s a m e p a c k a g e q Precision: if the subclass is not in the same package, instances of the subclass will not have access to the protected m e m b e r s for the instances of the superclass
R. Grin
page 23
R. Grin
page 24
Example
class A { . . . protected int m() { . . . } } class B extends A { . . . public int m2() { int i = m(); // always allowed A a = new A(); i += a.m(); // not always allowed . . . when is it allowed? }
R. Grin Java: inheritance and polymorphism page 25 R. Grin
Object class
page 26
Object class
q
toString method
q
java.lang.Object
Object
q Very useful for debugging q Must be overridden by almost all the classes
Object
Object
are:
toString() n equals()
n
R. Grin
page 27
R. Grin
page 28
equals method
q
public boolean equals(Object object) returns true if object is equal to this, the
current instance, according to an equality relation
f1.num
f2.num
obj1 == obj2
R. Grin
page 29
R. Grin
page 30
Example of hashCode
public int hashCode() { /* Reduce the fraction */ Fraction f = reduce(); return (17 + f.num * 37) * 37 + f.den; } }
a.d = b.cpage 31
R. Grin
page 32
getClass() method
q q
java.lang.Class
class
contains a
getName()
Polymorphism
R. Grin
page 33
R. Grin
page 34
A question
q Suppose that
Polymorphism
q It is the fact that the same call to a method in the code, can be executed by different methods q For instance,
A and that t h e method m() of B Which method m() will be executed in the following code, the one of A or of B ?
extends
A is
overridden in
A a = x.f(); a.m();
the method that will be executed can be the method m() of A or of any subclass of A
The method that is called depends only on the real type (B) of a (not the declared type, here A). So, the method from B will be executed
R. Grin Java: inheritance and polymorphism page 35
R. Grin
page 36
Example of polymorphism
Empty method
public class Figure { public void draw(Graphics g) {} Figure.java } public class Rectangle extends Figure { public void draw(Graphics g) { . . . Rectangle.java } public class Circle extends Figure { public void draw(Graphics g) { . . . Circle.java }
R. Grin Java: inheritance and polymorphism page 37
Advantage of polymorphism
q A good use of polymorphism can avoid a program with a lot of tests (if, switch/case) :
a.m() the
compiler must be
contains a
m()
that the
for (int i = 0; i < figures.length; i++) { if (figures[i] instanceof Rectangle) { . . . // Draw a rectangle } else if (figures[i] instanceof Circle { . . . // Draw a circle } . . . }
R. Grin Java: inheritance and polymorphism page 40
R. Grin
page 39
Advantage of polymorphism
q With polymorphism you can easily extend an application q For instance, if you add a new type of figure, you dont have to modify the code of the method draw of the figure:
Without polymorphism
q You will have to add code to draw the new type of
Drawing
class
public void draw(Graphics g) { for (int i=0; i < figures.length; i++) { figures[i].draw(g); } }
R. Grin Java: inheritance and polymorphism page 41 R. Grin Java: inheritance and polymorphism page 42
Conclusion
q Polymorphism is a fundamental issue in Object Oriented Programming and the basis for an efficient use of inheritance q Polymorphism is achieved by the late binding mechanism: the method that will be called is determined at run time, not at compilation time
Cast
R. Grin
page 43
R. Grin
page 44
R. Grin
page 45
R. Grin
page 46
Example of upcast
public class Engineer extends Employee { . . . } public class Project { public void add(Employee employee) { . . . } . . . } // somewhere else Ingineer i1 = new Ingineer(2563, "John"); project.add(i1);
R. Grin Java: inheritance and polymorphism page 47
R. Grin
page 48
Example of downcast
// Figures are added in an ArrayList ArrayList figures = new ArrayList(); figures.add(new Circle(center, radius)); figures.add(new Rectangle(p1, p2)); . . . // get(i) return an Object; a cast is // necessary to call the draw method ((Figure)figures.get(i) ).draw(g);
q q q q
Keyword final
final final final final
class: cannot have subclasses m e t h o d: cannot be overridden variable: cannot be modified after
method body
R. Grin
page 49
R. Grin
page 50
abstract)
when you
only give its signature and its return type, not its q A class must be declared abstract method q Abstract classes are useful for classifying classes or for code factorization
Abstract classes
abstract if
it contains an
R. Grin
page 51
R. Grin
page 52
Interfaces
R. Grin
page 53
R. Grin
page 54
Interfaces
q An interface is a 100% abstract class, without any variables q An interface can contain constants ( static
Example of interface
public interface Figure { public void draw(Graphics g); }
final )
q Methods and constants are necessarily (and implicitly) public q An interface can inherit (keyword from s e v e r a l interfaces
extends )
R. Grin
page 55
R. Grin
page 56
Implementation of an interface
q A class can implement (keyword one or s e v e r a l interfaces q If
implements)
C is
R. Grin
page 58
Example of an implementation
public class City implements Comparable { private String name; private int nbInhabitants; . . . public int compareTo(Object object) { return nbInhabitant s - (City)objet.nbInhabitants; } }
R. Grin
page 59
R. Grin
page 60
10
Using interfaces
q You can apply polymorphism with classes from different inheritance hierarchies if they implement a same interface q The advantage of interface is that a class can implement several interfaces
R. Grin
page 61
R. Grin
page 62
// upcast City Comparable Comparable c1 = new City("Cannes", 200000); Comparable c2 = new City("Nice", 500000);
. . .
Reuse
if (c1.compareTo(c2))
// downcast Comparable
// upcast Comparable
Object
City
R. Grin
page 63
R. Grin
page 64
n created
R. Grin
page 65
R. Grin
page 66
11
Advantages of inheritance
q Easy to use: basic mechanism of the language q Allow polymorphism (but also possible with interface) q Easy to understand if it represents a is - a relation q Flexible because you can modify inherited behavior (by overriding methods)
n Use n Use
Conclusion
q To reuse classes, inheritance to represent static is - a relations delegation and composition in the other
cases
R. Grin
page 67
R. Grin
page 68
12