Translate

Tuesday, 17 June 2014

super keyword

super Keyword

The super is a reference variable that is used to refer immediate parent class object.

Usage of super Keyword


  • super is used to refer immediate parent class instance variable.
  • super() is used to invoke immediate parent class constructor.
  • super is used to invoke immediate parent class method.

  • Problem without super keyword:-

    class Student{  
      int marks=50;  
    }  
      
    class CseStudent extends Student{  
      int marks=100;  
          
      void display(){  
       System.out.println(marks);//will print marks of cse students   
      }  
      public static void main(String args[]){  
       CseStudent cse = new CseStudent();  
       cse.display();  
         
    }  
    }  
    output: 100

    super keyword is used to refer the parent class instance variable

    class Vehicle{  
      int speed=40;  
    }  
      
    class Bike extends Vehicle{  
      int speed=100;  
          
      void display(){  
       System.out.println(super.speed);//will print speed of Vehicle now  
      }  
      public static void main(String args[]){  
       Bike b=new Bike();  
       b.display();  
         
    }  
    }  
    output: 40

    super keyword is used to invoke parent class constructor

    super() must be the first line of constructor and if programmer don't call the constructor,compiler wil automaticlly(imlicitly) call the constructor.

    Example

    class Vehicle{
              Vehicle()
     {
        System.out.println("Vehicle is created");
     }
    }
     
    class Bike5 extends Vehicle{
              Bike(){
              super();//will invoke parent class constructor
              System.out.println("Bike is created");
    }
      public static void main(String args[]){
       Bike b=new Bike();
          }
    }  

    output: Vehicle is created
                Bike is created

    super keyword is used to invoke the parent class method

    Example:

    class Person{  
                void message()
    {
       System.out.println("welcome");
    }  
    }  
      
    class Student extends Person{  
                void message()
    {
      System.out.println("welcome to java");
    }  
      
                void display()
    {  
                   message();//will invoke current class message() method  
                   super.message();//will invoke parent class message() method  
    }  
      
            public static void main(String args[]){  
            Student s=new Student();  
            s.display();  
        }  

    }  

    output: welcome to java
                welcome

    In the above example Student and Person both classes have message() method if we call message() method from Student class, it will call the message() method of Student class not of Person class because priority is given to local.
    In case there is no method in subclass as parent, there is no need to use super.

    No comments:

    Post a Comment