Chapter 3
Object Oriented
Programming
Teacher: Kadache Nabil
Year: 2023/2024
1
Part 2
Inheritance and
Polymorphism
2
Inheritance & Polymorphism
Plan
Definition
Redefinition of methods
Inheritance and abstract classes
Polymorphism
3
Inheritance and Polymorphism (1)
Inheritance is a principle specific to object-oriented programming,
allowing you to create a new class from an existing class.
The inheriting or derived class (the newly created class) implicitly
contains all the attributes and methods of its superclass (the class from
which it derives).
The interest of inheritance:
Easy reuse of classes.
Class specialization (extensibility) by adding
new features (attributes and Methods).
4
Inheritance and Polymorphism (2)
The type defined by a class is a subtype of the type defined by its
superclass, this relationship is expressed by the term "is -a " .
Simple inheritance: inheritance from at most a single class (simplicity).
Multiple inheritance: inheritance from several classes (richness vs
ambiguity).
5
Inheritance and Polymorphism (3)
UML notation: classB inherits from classA , everything that is in classA like
attributes and methods:
classA
Private Int Attr1
Private Int Attr2
Public void m1(…)
Publiv int m2(….)
classB
Private Int Attr3
Public void m3(…)
Publiv float m4(….)
6
Inheritance and Polymorphism (4)
The type defined by the class classB is included in the type defined by the
class
Interesting property : any object of class classB can be considered as
Object of class classA too (the opposite is not always true).
Type( classA )
Type( classB )
7
Inheritance and Polymorphism (5)
In Java: no multiple inheritance, by default a class inherits from the
parent class of all Java “Object” classes.
Redefinition of methods: when a method is defined in a Class, it
can be redefined in subclasses (inheriting classes) with the same
signature and a new implementation
Interest: Algorithmic evolution
Disadvantage: Loss of functionality (in this case, you have to use
the old implementation in the redefined method).
8
Inheritance and Polymorphism (6)
Inheritance and abstract classes:
Abstract class: doesn't produce objects, doesn't have objects in the
actual application:
Among the interests of the classes
abstract: Factorization
Example:
class B {
Attributes: att1 , att2 , att3
Methods: m1 ,m2
}
class C {
Attributes: att1 , att2 , att4 // att1 and att2 are the same type as in B
Methods: m1 ,m3 // m1 has the same signature as in B
// and a possibly different implementation
}
9
Inheritance and Polymorphism (7)
In order to avoid the redundancy of attributes and methods which
have the same meaning of the two classes we can carry out a
factorization as follows:
class A {
Attributes: att1,att2
Abstract method m1 //
}
10
Inheritance and Polymorphism (8)
class B extends A {
Attributes: att3
Methods: m1 ,m2 // m1 redefined with implementation of B
}
class C extends A {
Attributes: att4
methods: m1 ,m3 // m1 redefined with C implementation
}
11
Inheritance and Polymorphism (9)
Polymorphism:
Polymorphic = having several forms.
In OOP a polymorphic method is a method that has several
implementations (body).
Ex:
An overloaded method: weak polymorphism (in reality several distinct
methods).
A method redefined in a hierarchy of classes linked by inheritance
(inheritance polymorphism)
A generic method (parametric polymorphism)
12
Inheritance and Polymorphism (10)
Polymorphism:
A polymorphic object is an object which contains polymorphic
methods.
Typing of objects :
The declared Type: Defines all the operations applicable to an object;
can be checked during compilation
The Current Type: this is a dynamic type that can only be known at
runtime.
13
Inheritance and Polymorphism (11)
Example :
The Bird class inherits from the Animal class
Animal a1 = new Animal();
Bird o1 = new Bird();
Animal o2 = new Bird();
----------- ------------
Declared Current
type type (dynamic type)
14
Inheritance and Polymorphism (12)
Interesting property : when a polymorphic method of an object is
invoked, it is that of its dynamic type that is invoked.
Interest: Writing service code (classes) invariant to the service usage
code using super-type invocation (Transparent evolution and
without impact on user code).
This property involves link editing at run time (dynamic linking)
unlike static link editing (done with compiled modules to obtain an
executable)
In Java= all methods are dynamically bound (virtual)
In C++ = left to the programmer's choice (explicit by the keyword
virtual in front of the header of a method).
15