Chapter 3 Part2 - Object Oriented Programming
Chapter 3 Part2 - Object Oriented Programming
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)
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
4
Inheritance and Polymorphism (2)
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
classB
Private Int Attr3
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)
8
Inheritance and Polymorphism (6)
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:
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)
15