Skip to main content

Super and Final keyword

 


Super Keyword

    The super keyword in java is a reference varible which is use to refer immediate parent class instance variable. Super can use to invoke immediate parent class method and constructors also.


1. To Refer Instance variable of an immediate parent class:
    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. For example,
class Animal{  
String color="white";  
}  
class Dog extends Animal{  
String color="black";  
void printColor(){  
System.out.println(color);
System.out.println(super.color);
}  
}  
class TestSuper1{  
public static void main(String args[]){  
Dog d=new Dog();  
d.printColor();  
}
}
Output:
black
white

2. To Refer method of an immediate parent class:
    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. For example,
class Animal{  
void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal{  
void eat(){System.out.println("eating bread...");}  
void bark(){System.out.println("barking...");}  
void work(){  
super.eat();  
bark();  
}  
}  
class TestSuper2{  
public static void main(String args[]){  
Dog d=new Dog();  
d.work();  
}
}     
Output:
eating...
barking...

3. To Refer constructor of an immediate parent class
    The super keyword can also be used to invoke the parent class constructor. Let's see a simple example:
class Animal{  
Animal(){System.out.println("animal is created");}  
}  
class Dog extends Animal{  
Dog(){  
super();  
System.out.println("dog is created");  
}  
}  
class TestSuper3{  
public static void main(String args[]){  
Dog d=new Dog();  
}
}  
Output:
animal is created
dog is created

Final

    Final keyword is use to restrict the user. 

We can use this keyword with:

  1. Variable
  2. Method
  3. Class
1. With Variable:
    If we use final keyword with variable then the value of that variable can not be change.(It will be the constant). For example,

class Bike9{
final int speedlimit=90;
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}

Output:
Output:Compile Time Error

2. With Method:
    If you make any method as final, you cannot override it. For example,
class Bike{
  final void run(){System.out.println("running");}
}
   
class Honda extends Bike{
   void run(){System.out.println("running safely with 100kmph");}
   
   public static void main(String args[]){
   Honda honda= new Honda();
   honda.run();
   }
}
Output:
Output:Compile Time Error

3. With Class:
    If you make any class as final, you cannot extend it. For example,
final class Bike{} 
 class Honda1 extends Bike{
   void run(){System.out.println("running safely with 100kmph");}
   
   public static void main(String args[]){
   Honda1 honda= new Honda();
   honda.run();
   }
 }
Output:
Output:running...

Comments