What is super keyword in java

The super keyword refers to super class (parent) objects. It is used to call super class methods, and to access the super class constructor. The most common use of the super keyword is to eliminate the confusion between super classes and sub classes that have methods with the same name. The super keyword is used to access hidden fields and overridden methods or attributes of the parent class.

Following are the cases when this keyword can be used:

  • Super with variable: Accessing data members of parent class when the member names of the class and its child sub classes are same. In given below example we have define same name variable in drive class and sub-class and its seems to ambiguity in code but using the super keyword we can call drive and sub-class variable easily.

class Parent {
    String msg = "Hi Parent";

    public void hello() {
        System.out.println("Parent class hello()");
    }
}

class Child extends Parent {
    String msg = "Hi Child";

    public void hello() {
        super.hello(); // call here parent class
        System.out.println(super.msg); // super class msg calling
        System.out.println("Child class hello()");
        super.hello(); // call here parent class hello again
        System.out.println(msg); // child class msg calling

    }
}

public class SuperKeyword {
    public static void main(String[] args) {
        Child child = new Child();
        child.hello();
    }
}

Parent class hello() Hi Parent Child class hello() Parent class hello() Hi Child


  • Super with constructor: To call the default and parameterized constructor of the parent class inside the child class. One more thing calling of constructor of drive class in sub-class super will be first statement we can't call super class constructor after any statement. See in given below example how is working.

class Parent {
    Parent(){
        System.out.println("Parent class constructor calling...");
    }
    public void hello() {
        System.out.println("Parent class hello()");
    }
}

class Child extends Parent {
    Child(){
        super(); // parent class constructor calling
        System.out.println("Child class constructor calling...");
//      super(); // after any statement we can't call super class constructor
    }

    public void hello() {
   
        super.hello(); // call here parent class
        super.hello(); // call here parent class hello again

    }
}


  • Super with method: Accessing the parent class methods when the child classes have overridden them. If we have same name method in super class and sub-class and its seems to method ambiguity but we can call super class method using super keyword.


class Parent {
    public void hello() {
        System.out.println("Parent class hello()");
    }
}

class Child extends Parent {
      public void hello() {
        super.hello(); // call here parent class
        System.out.println("Child class hello()");
        super.hello(); // call here parent class hello again
      }
}

public class SuperKeyword {
    public static void main(String[] args) {
     Child child=new Child();
     child.hello();
    }
}

Parent class hello() Child class hello() Parent class hello()



No comments:

Post a Comment