super Keyword Java

super Keyword Java

We know earlier, in method overriding, the subclass method supersedes the super class method. When the method is overridden in inheritance, the subclass object calls its own method. If it calls its own method, the subclass looses the functionality of the super class method (of course, subclass is at liberty). If the subclass would like to call the super class method also, the subclass can do so using "super" keyword. "super" keyword is used by the subclass to call that of super class also when the methods or variables are overridden. "super" keyword can be used with instance variables and methods but not with classes.

1. super with Methods

In the following program, subclass overrides the eat() method of super class and at the same uses both with "super" keyword.

super Keyword Java

Output screen of Sparrow.java


The eat() method exists in both Bird and Sparrow classes. We say, super class method is overridden by subclass. s1.eat() calls its own (Sparrow) method. Sparrow uses "super.eat();" to call Bird's eat() method. The next program illustrates "static" with variables.

Note:

  1. "super" keyword cannot be used from static methods like main().
  2. Static methods cannot be overridden (cannot be called with super keyword).

2. super with Variables

You have seen earlier "super" with methods. Let us go for with variables.

super Keyword Java

Output screen of TotalCost.java


In the above code, cost variable of Packing class is overridden by TotalCost. In this case, the subclass object prefers to call its own variable. super.cost calls super class cost variable. super.cost cannot be called from static methods like main() method.

Programming Tip: Do not attempt to use super keyword with classes. It is a compilation error.

"super" and "this"

In java, "super" keyword is used to call super methods and variables (when overridden only, else, not necessary to use) and "this" keyword is used to refer the current object.

8 thoughts on “super Keyword Java

  1. vishvapriya dixit

    Sir if i write only cost=100; insteda of int cost=100; then it provide the result 100 if i use super on not in both case why ?

  2. hari krishna prasad.K

    static methods are possible to override,taken example main() are override with distinct parameters ,its is possible but only main(String args[]) are directly called by jvm,remaining main() methods are called by programmer itself it acts like a normal methods.

  3. Manjunath Salagar

    Q.Why can I not use “super” variable from a static context, even though “super” refers to the parent class and NOT a class instant, unlike “this”?

    My Ans: Because we will not give any guarantees to compiler assigning memory to non static member(Non static members like variable, method will get memory only after creating object its class ).

    => Is my thought is correct? Please let me.

    1. S. Nageswara Rao, Sr.Faculty

      super keyword cannot be used from static methods. Perhaps, you are calling it from static main. Check.

Leave a Reply

Your email address will not be published. Required fields are marked *