Final And Super Keyword In Java

---------------------------------------------------------------------- 

Final Keyword In Java:- 


The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be:

  1. variable
  2. method
  3. class

The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let's first learn the basics of final keyword.

1) Java final variable

If you make any variable as final, you cannot change the value of final variable(It will be constant).

Example of final variable

There is a final variable speedlimit, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.

1.       class Bike9{  

  1.  final int speedlimit=90;//final variable  
  2.  void run(){  
  3.   speedlimit=400;  
  4.  }  
  5.  public static void main(String args[]){  
  6.  Bike9 obj=new  Bike9();  
  7.  obj.run();  
  8.  }  
  9. }//end of class  

 

Output:Compile Time Error

2) Java final method

If you make any method as final, you cannot override it.

Example of final method

1.     class Bike{  

  1.   final void run(){System.out.println("running");}  
  2. }  
  3.      
  4. class Honda extends Bike{  
  5.    void run(){System.out.println("running safely with 100kmph");}  
  6.      
  7.    public static void main(String args[]){  
  8.    Honda honda= new Honda();  
  9.    honda.run();  
  10.    }  
  11. }  

 

Output:Compile Time Error

3) Java final class

If you make any class as final, you cannot extend it.

Example of final class

1.     final class Bike{}  

2.     class Honda1 extends Bike{  

3.     void run(){System.out.println("running safely with 100kmph");}  

4.     public static void main(String args[]){  

5.     Honda1 honda= new Honda1();  

6.     honda.run();  

7.     }  

8.     }  

Output:Compile Time Error

 

 -------------------------------------------------------------------------------

   Super Keyword in Java:- 

The super keyword in Java is a reference variable which is used to refer immediate parent class object.

Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.

Usage of Java super Keyword

  1. super can be used to refer immediate parent class instance variable.
  2. super can be used to invoke immediate parent class method.
  3. super() can be used to invoke immediate parent class constructor.

1) super is used to refer immediate parent class instance variable.

We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields.

1.     class Animal{  

2.     String color="white";  

3.     }  

4.     class Dog extends Animal{  

5.     String color="black";  

6.     void printColor(){  

7.     System.out.println(color);//prints color of Dog class  

8.     System.out.println(super.color);//prints color of Animal class  

9.     }  

10.  }  

11.  class TestSuper1{  

12.  public static void main(String args[]){  

13.  Dog d=new Dog();  

14.  d.printColor();  

15.  }}  

Output:

black
white

In the above example, Animal and Dog both classes have a common property color. If we print color property, it will print the color of current class by default. To access the parent property, we need to use super keyword.

2) super can be used to invoke parent class method

The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overridden.

1.     class Animal{  

2.     void eat(){System.out.println("eating...");}  

3.     }  

4.     class Dog extends Animal{  

5.     void eat(){System.out.println("eating bread...");}  

6.     void bark(){System.out.println("barking...");}  

7.     void work(){  

8.     super.eat();  

9.     bark();  

10.  }  

11.  }  

12.  class TestSuper2{  

13.  public static void main(String args[]){  

14.  Dog d=new Dog();  

15.  d.work();  

16.  }}  

Test it Now

Output:

eating...
barking...

In the above example Animal and Dog both classes have eat() method if we call eat() method from Dog class, it will call the eat() method of Dog class by default because priority is given to local.

To call the parent class method, we need to use super keyword.

3) super is used to invoke parent class constructor.

The super keyword can also be used to invoke the parent class constructor. Let's see a simple example:

1.     class Animal{  

2.     Animal(){System.out.println("animal is created");}  

3.     }  

4.     class Dog extends Animal{  

5.     Dog(){  

6.     super();  

7.     System.out.println("dog is created");  

8.     }  

9.     }  

10.  class TestSuper3{  

11.  public static void main(String args[]){  

12.  Dog d=new Dog();  

13.  }}  

Output:

animal is created
dog is created   

The End

No comments:

Post a Comment

Aarsh Prajapati

Welcome! Java Programs And Theory  Practical    Theory Java Basic Program Java Simple Program Array, Inheritance and Interface Program Packa...