super keyword in Java

super is one of the heavily used keywords in Java programming language. It is used in the context of inheritance to refer the direct superclass of a subclass. It has two forms: super() and super. While deisgning a class:

  1. super() calls the constructor of the direct superclass.
  2. super refers to the direct superclass. So by using super keyword you can access the methods and instance variables of the direct superclass.

1. Uses of super()

It has only one use: that is to call the constructor of the direct superclass. It is generally used to initialize the fields inherited from the superclass. If used, it must be the first statment is a subclass constructor.

  • Depending on the need, you can supply parameters to super as super(parameter-list) or suppy no parameters at all like super().

Output :

 


  • super(parameter-list) calls the constructor of the superclass that matches the parameter-list. The basis for matching is number and type of the parameters in the parameter-list.

Output :


  • If subclass doesn’t call the superclass constructor explicitly, the compiler inserts parameter-less super() as the first statement inside the subclass constructor. If the superclass doesn’t have a parameter-less constructor, you will get compilation error.

Output :

In both the subclass constructors, parameter-less superclass constructor was called.

Note: Like classes, super() can be chained. super() calls the immediate superclass which can have super() that in turn calls its immediate superclass and so on.


2. Uses of super

Super can acess the public and protected members of the superclass. (private members stay hidden)

  • super can be used to access the instance variables of the superclass.
  • super can be used to acess the methods defined in the superclass. As such it can be used to call the overridden methods of the superclass class.
2.1 super can be used to access the instance variables of the superclass.

Output :

taste field is only defined in the superclass. The statement  taste = "Sweet"; assigns value “Sweet” to variable taste. Then statement  super.taste = "Sour"; assigns value “Sour” to the same variable taste.

The variable taste is defined in the superclass but you can access it without using super because there is no variable with the same name in the subclass.


2.2 super can be used to access the overridden methods of the superclass.

If a method of the superclass is overriden in the subclass, you can access the superclass method by using the super keyword(as super.method_name()).

Output :

Method getFeture() is overridden in the subclass. So by calling getFeature();  , you call the method defined inside the subclass. By calling  super.getFeature(); , you call the method defined inside the superclass.

Method getColor() is only defined inside the superclass. So you can access it even without using the super keyword. So both of the method calls  getColor();  and  super.getColor(); result in calling the same method definition.


3. Points to Remember

  • You cannot use this() and super() inside the same constructor. This is because both of them need to be first statement inside the constructor.