super keyword in Java

The super is a keyword defined in the java programming language. Keywords are basically reserved words which have specific meaning relevant to a compiler in java programming language likewise the super keyword indicates the following :


  1. The super keyword in java programming language refers to the superclass of the class where the super keyword is currently being used.
  2. The super keyword as a standalone statement is used to call the constructor of the superclass in the base class.
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass:
public class Superclass {

    public void printMethod() {
        System.out.println("Printed in Superclass.");
    }
}

Here is a subclass, called Subclass, that overrides printMethod():

public class Subclass extends Superclass {

    // overrides printMethod in Superclass
    public void printMethod() {
        super.printMethod();
        System.out.println("Printed in Subclass");
    }
    public static void main(String[] args) {
        Subclass s = new Subclass();
        s.printMethod();    
    }
}

Within Subclass, the simple name printMethod() refers to the one declared in Subclass, which overrides the one in Superclass. So, to refer to printMethod() inherited from Superclass, Subclass must use a qualified name, using super as shown. Compiling and executing Subclass prints the following:

Output:
Printed in Superclass.
Printed in Subclass

super is used to invoke parent class constructor
The super keyword can also be used to invoke the parent class constructor as given below:
class Vehicle{
  Vehicle(){
    System.out.println("Vehicle is created");
   }
}

class Bike 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

Note:super() is added in each class constructor automatically by compiler.

As we know well that default constructor is provided by compiler automatically but it also adds super() for the first statement.If you are creating your own constructor and you don't have either this() or super() as the first statement, compiler will provide super() as the first statement of the constructor.



Another example of super keyword where super() is provided by the compiler implicitly.


class Vehicle{
  Vehicle(){
   System.out.println("Vehicle is created");
  }
}

class Bike extends Vehicle{
  int speed;
  Bike(int speed){
    this.speed=speed;
    System.out.println(speed);
  }
  public static void main(String args[]){
   Bike b=new Bike(10);
      
}
}

Output:
Vehicle is created
10



<<Previous <<   || Index ||   >>Next >>


2 comments:

  1. I'm curious to find out what blog system you're using?
    I'm having some small security problems with my latest blog and I would like to find something more safeguarded. Do you have any suggestions?

    My web blog: 500 fast cash

    ReplyDelete
  2. I like the valuable info you provide in your articles. I will bookmark your weblog and check again here regularly.
    I'm quite certain I will learn many new stuff right here! Good luck for the next!

    Also visit my blog post ... jason statham Workout

    ReplyDelete