Java >> Tutoriel Java >  >> Tag >> final

Utilisation du mot-clé final avec héritage en Java

mot-clé final avec héritage en java

  • Le mot clé final est définitif, c'est-à-dire que nous ne pouvons pas changer.
  • Nous pouvons utiliser les mots clés finaux pour les variables, les méthodes et la classe.
  • Si nous utilisons le mot-clé final pour l'héritage c'est-à-dire si nous déclarons une méthode avec le mot-clé final dans la classe de base donc l'implémentation de la méthode finale sera le même que dans la classe dérivée.
  • Nous pouvons déclarer la méthode finale dans toute sous-classe pour laquelle nous voulons que si une autre classe étend cette sous-classe.

Cas 1 :Déclarer la variable finale avec héritage

// Declaring Parent class
class Parent {
    /* Creation of final variable pa of string type i.e 
    the value of this variable is fixed throughout all 
    the derived classes or not overidden*/
    final String pa = "Hello , We are in parent class variable";
}
// Declaring Child class by extending Parent class
class Child extends Parent {
    /* Creation of variable ch of string type i.e 
    the value of this variable is not fixed throughout all 
    the derived classes or overidden*/
    String ch = "Hello , We are in child class variable";
}

class Test {
    public static void main(String[] args) {
        // Creation of Parent class object
        Parent p = new Parent();
        // Calling a variable pa by parent object 
        System.out.println(p.pa);
        // Creation of Child class object
        Child c = new Child();

        // Calling a variable ch by Child object 
        System.out.println(c.ch);
        // Calling a variable pa by Child object 
        System.out.println(c.pa);
    }
}

Sortie

D:\Programs>javac Test.java
D:\Programs>java Test
Hello , We are in parent class variable
Hello , We are in child class variable
Hello , We are in parent class variable

Cas 2 :Déclarer des méthodes finales avec héritage

// Declaring Parent class
class Parent {
    /* Creation of final method parent of void type i.e 
    the implementation of this method is fixed throughout 
    all the derived classes or not overidden*/
    final void parent() {
        System.out.println("Hello , we are in parent method");
    }
}
// Declaring Child class by extending Parent class
class Child extends Parent {

    /* Creation of final method child of void type i.e 
    the implementation of this method is not fixed throughout 
    all the derived classes or not overidden*/
    void child() {
        System.out.println("Hello , we are in child method");
    }
}

class Test {
    public static void main(String[] args) {
        // Creation of Parent class object
        Parent p = new Parent();
        // Calling a method parent() by parent object 
        p.parent();
        // Creation of Child class object
        Child c = new Child();
        // Calling a method child() by Child class object 
        c.child();
        // Calling a method parent() by child object 
        c.parent();
    }
}

Sortie

D:\Programs>javac Test.java
D:\Programs>java Test
Hello , we are in parent method
Hello , we are in child method
Hello , we are in parent method

Balise Java