Académique Documents
Professionnel Documents
Culture Documents
POO en Java
Ch. VIII
L'héritage
L'héritage est un mécanisme qui facilite la réutilisation
du code et la gestion de son évolution.
1
12/05/2023
2
12/05/2023
L'héritage: extends
Mise en œuvre de l'héritage :
3
12/05/2023
4
12/05/2023
class A class C
public int x;
private int y;
int z;
protected w;
class B class D
5
12/05/2023
class A class C
public void meth1(){};
private void meth2(){};
void meth3(){};
protected void meth4(){};
class B class D
Package 1 Package 2
class A class C
public int x;
private int y;
int z;
protected w;
class B class D
private int y La variable private n'est accessible que depuis l'intérieur même de la
classe.
6
12/05/2023
L'héritage: super
7
12/05/2023
ATTENTION
l'appel à super(…)
se fait en premier
avant l’initialisation
des attributs de
la classe fille.
L'héritage
Remarques :
8
12/05/2023
Solution(s) ???
9
12/05/2023
La Redéfinition: overrinding
Les sous-classes peuvent redéfinir les variables
et les méthodes héritées :
Pour les variables : il suffit de les redéclarer
sous le même nom avec un type différent.
10
12/05/2023
Let's understand the problem that we may face in the program if we don't use
method overriding.
11
12/05/2023
In this example, we have defined the run method in the subclass as defined in the
parent class but it has some specific implementation. The name and parameter of
the method are the same, and there is IS-A relationship between the classes, so
there is method overriding.
Exemple1:
Redéfinition Méthode
12
12/05/2023
La Redéfinition: Exemple 2
public class Employe { public Employe(int code, String nom) {
public Employe() { }
} }
this.nom = nom; }
} }
La Redéfinition: Exemple 2
public class Secretaire extends Employe{
private String code; //redéfinition de l’attribut code
public Secretaire(String code , String nom) {
super(nom);
this.code = code;
}
@Override
public void afficher(){
System.out.println("Code= "+this.code+", Nom ="+this.getNom());
}
}
13
12/05/2023
La Redéfinition: Exemple 2
Code=2021-80A, Nom=Samri
14
12/05/2023
La Redéfinition: remarques
15
12/05/2023
16
12/05/2023
Surcharge:Exemple
17
12/05/2023
18
12/05/2023
Le polymorphisme
Polymorphisme
19
12/05/2023
20
12/05/2023
21