Inheritance and Polymorphism
• Inheritance
• Polymorphism
• Abstract classes
• Interfaces
DR. YOUSSEF ELMIR ELMIR@[Link]
Inheritance and Polymorphism
2
INHERITANCE
Inheritance and Polymorphism
3
The concept of inheritance
Let's suppose we have the Point class seen in the previous
course, and we want to create a PointCol class to
manipulate coloured points on a plane.
The PointCol class can have the same attributes and
functionalities as the Point class, to which we add a
colour attribute representing the colour of a point and a
colorize method to assign a colour to a point.
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
4
The concept of inheritance
The definition of the PointCol class:
class PointCol extends Point
{
private String colour;
public void colorise (String colour)
{
[Link] = colour;
}
}
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
5
The concept of inheritance
The definition of the PointCol class:
class PointCol extends Point
{
private String colour;
public void colorise (String colour)
{
[Link] = colour;
} Specify to the
} compiler that the
PointCol class
derives from the
Point class.
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
6
The concept of inheritance
An object of type PointCol can then invoke
Thepublic methods of PointCol: colorise
And the public methods of the Point class:
move and display
An object of a derived class accesses the
public members of its base class
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
7
A complete example
A class using an object of type PointColclass Test
{
public static void main(String[] args)
{
PointCol pc = new PointCol ();
[Link](); Coordinates: 0 0
[Link]("Black");
[Link]();
Coordinates: 0 0
[Link](2, 5);
[Link]();
Coordinates: 2 5
}
}
What will the program display?
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
8
Access of a derived class to members of its base class
class PointCol extends Point
{
private String colour;
public void colorise (String Colour)
{
[Link] = colour;
}
public void displayAll()
{
[Link](" Coordinates : " + x +" "+ y);
[Link](" Colour: " + colour );
}
}
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
9
Access of a derived class to members of its base class
class PointCol extends Point
{
private String colour;
public void colorise (String Colour)
{
[Link] = colour;
}
public void displayAll()
{
[Link](" Coordinates : " + x +" "+ y);
[Link](" Colour: " + colour );
}
}
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
10
Access of a derived class to members of its base class
class PointCol extends Point
{
private String colour;
public void colorise (String Colour)
{
[Link] = colour;
}
public void displayAll()
{
display();
[Link](" Colour: " + colour );
}
}
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
11
Access of a derived class to members of its base class
Class point {
protected int x;
protected int y;
…
}
class PointCol extends Point
{
private String colour
public void displayAll()
{
[Link]("Coordonates : " + x + " " +y);
[Link](" Colour: " + colour );
}
}
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
12
Access of a derived class to members of its base class
Rule 1:
A method of a derived class does not access
private members of its base class.
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
13
Constructing derived objects:
1. Initialize the three fields: x, y, and colour.
2. Use the constructor of the base class Point.
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
14
Constructing derived objects:
1. Initialize the three fields: x, y, and colour.
class PointCol extends Point
{
private String colour;
public PointCol (int x, inty, String colour)
{
setX(x);
setY(y);
[Link] = colour
}
// other methods
}
1.
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
15
Constructing derived objects:
2. Use the constructor of the base class Point.
Class PointCol extends Point
{
private String colour;
public PointCol (int x, int y, String colour)
{
super(x,y);
[Link] = colour;
}
// other methods
}
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
16
Constructing derived objects:
Case 1: If the base class has no constructor
It is possible to call the default constructor of the base class.
Class A
{….// no constructor
}
Class B extends A
{
public B(..)// constructor of B
{
super();
…
}
}
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
17
Constructing derived objects:
Case 2: If the derived class has no constructor
There are two possible scenarios:
1. If the base class has a parameterized constructor, it must also have a default
constructor that will be called by the default constructor of the derived class.
2. If the base class has no constructor, there is no issue; its default constructor will be
called.
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
18
Constructing derived objects:
Example 1
Class A{
public A(){…} // Constructor 1 of A
public A(int n) // Constructor 2 of A
}
Class B extends A
{
…// no constructor
}
How is an object of B created?
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
19
Constructing derived objects:
Example 1
Class A{
public A(){…} // Constructor 1 of A
public A(int n) // Constructor 2 of A
B b = new B();
}
The default constructor of
Class B extends A class B calls constructor 1
{ of class A.
…// no constructor
}
How is an object of B created?
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
20
Constructing derived objects:
Example 2
Class A
{public A (int n) //Constructor of A
}
Class B extends A
{
…// no constructor
}
How is an object of B created?
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
21
Constructing derived objects:
Example 2
Class A Compilation error!
{public A (int n) //Constructor of A The default constructor of B
}
tries to find a parameterless
constructor of A.
Class B extends A
Since A has a constructor with
{
arguments, it's no longer
…// no constructor
possible to use the default
} constructor of A.
How is an object of B created?
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
22
Constructing derived objects:
Example 3
Class A
{… // no constructor
}
Class B extends A
{
…// no constructor
}
How is an object of B created?
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
23
Constructing derived objects:
Example 3
Class A
{… // no constructor B b = new B();
}
Class B extends A The default constructor of B
{ calls the default constructor of
…// no constructor A.
}
How is an object of B created?
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
24
Constructing derived objects:
The derived class has a
constructor
The base class has a
constructor
The base class has no
constructor: We can use
super without
parameters or not use it
With arguments: Without arguments: (the default constructor
The constructor of the We can use super is called)
derived class must call without parameters or
that of the base class not use it (it will be
with the keyword super called by default)
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
25
Constructing derived objects:
The derived class does not
have a constructor
The base class has a
constructor
The base class has no
constructor: no
With arguments: problem arises, the
default constructor of
it must also have a Without arguments: the derived class calls
constructor without the default
The default
arguments that will be constructor of the
constructor of the
called by the default base class.
derived class will call
constructor of the
this constructor
derived class,
without arguments
otherwise we will have
a compilation error
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
26
Initialization of a derived object
In a derived class (class B extends A {...}), the creation of an object occurs as
follows:
1. Memory allocation for an object of type B
2. Implicit initialization of all fields of B
3. Explicit initialization of inherited fields from A (if any)
4. Execution of the body of the constructor of A
5. Explicit initialization of fields specific to B (if any)
6. Execution of the body of the constructor of B
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
27
Successive derivations
In Java, multiple inheritance does not exist. A class can only derive from one and
only one class.
Several different classes can be derived from the same class. Therefore, a derived
class can be the base class for another derived class.
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
28
Method Overriding
A derived class can override a method from a base class (direct or indirect).
The overridden method can only be used by the derived class or its descendants (its
derived classes) but not by its ancestors.
A derived class can also redefine a method, meaning it can modify the body of
instructions of a method without changing its signature or return type. In this case,
the new method replaces the method of the base class.
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
29
Method Overriding
Example of Point and PointCol classes:
class PointCol extends Point
{
private String colour;
… // other methods
public void display(){
[Link]();
[Link](" colour: " + colour)
}
}
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
30
Method Overriding
Example of Point and PointCol classes:
class PointCol extends Point
{
private String colour;
… // other methods
public void display(){
[Link]();
[Link](" colour: " + colour)
}
}
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
31
Method Overriding
Example of Point and PointCol classes:
class PointCol extends Point The call to the display method will
{ trigger a recursive call to the display
method of the PointCol class.
private String colour;
To specify that we want to call the
… // other methods display method of the Point class,
public void display(){ we use the keyword "super".
[Link]();
[Link](" colour: " + colour)
}
}
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
32
Method Overriding
Consider a method f defined in class A and overridden in classes C and D.
• Calling f in class A results in invoking f from class A.
• Calling f in class B results in invoking f from class A.
• Calling f in class C results in invoking f from class C.
• Calling f in class D results in invoking f from class D.
• Calling f in class E results in invoking f from class A.
• Calling f in class F results in invoking f from class C.
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
33
Overloading and inheritance
A class can overload a method from a base class
Example
class A calls f from A
{public void f (int n) {…}
….. Compilation error!
}
Class B extends A
calls f from A
{ public void f (float x) {….}
…
} calls f from B
A a; B b;
int n, float x;
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
34
Simultaneous use of overriding and overloading
The coexistence of overriding and overloading can lead to
complex situations that are best avoided by designing classes
carefully.
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
35
Simultaneous use of overriding and overloading
The overriding of a method should not reduce its access
rights. On the contrary, it can increase them.
Dr. Youssef Elmir elmir@[Link]
Inheritance and Polymorphism
36
Duplication of Fields
A derived class can define a field with the same name as a field in a superclass.
The new field is added to the existing one. Both fields can be accessed using the
`super` keyword to refer to the field of the superclass.
Dr. Youssef Elmir elmir@[Link]