• By using super keyword we can access super class variables and super class methods in sub class.
  • "Super" always refers to super class object.
  • One more interesting point about super is we can call super call constructors from sub class constructor using super keyword.
  • By default in sub class constructor super() call will be added by jvm.
  • We can also explicitly mention super call but rule is super() call must be first statement of the sub class constructor.
  • Let us see how sub class constructor calls super class constructors automatically. 


Java example program to demonstrate super call execution from sub class constructor to super class constructor 


  1. package com.superkeywordinjava;
  2.  public Class SuperDemo{ 
  3.  
  4. SuperDemo(){
  5. System.out.println("Inside super class constructor");
  6. }
  7.  
  8. }

  1. package com.superkeywordinjava;
  2. public Class Subdemo extends SuperDemo{ 
  3.  
  4. Subdemo(){
  5. System.out.println("Inside sub class constructor");
  6. }
  7.  
  8. public static void main (String args[]) {
  9.  Subdemo obj= new Subdemo();
  10.  
  11.  
  12. }
  13. }

Output:

  1. Inside super class constructor
  2. Inside sub class constructor


  • In the above programs whenever we are creating object of the sub class corresponding sub class constructor will be executed 
  • By default in sub class constructor super(); call will be added so now it will call super class zero argument constructor.

  1. Subdemo(){
  2. super(); // will be added automatically
  3. System.out.println("Inside sub class constructor");
  4. }

  • Whenever there is no constructor in sub class then default constructor added automatically and that contains super call in it.
  • And also when ever we define a constructor in our class .By default super(); call will be added automatically as first statement.
Java example program to demonstrate super call execution from sub class constructor to super class constructor by placing super call explicitly.


  1. package com.superkeywordinjava;
  2.  public Class SuperDemo{ 
  3.  
  4. SuperDemo(){
  5. System.out.println("Inside super class constructor");
  6. }
  7.  
  8. }

  1. package com.superkeywordinjava;
  2. public Class Subdemo extends SuperDemo{ 
  3.  
  4. Subdemo(){
  5. super();
  6. System.out.println("Inside sub class constructor");
  7. }
  8.  
  9. public static void main (String args[]) {
  10.  Subdemo obj= new Subdemo();
  11.  
  12.  
  13. }
  14. }

Output:

  1. Inside super class constructor
  2. Inside sub class constructor

  • Super call must be first statement inside the sub class constructor otherwise compile time error will come.
super keyword in java inheritance


  • If super class having parameterized constructor it is mandatory to call super class constructor explicitly by passing required parameters.
  • other wise compile time error will come

super in inheritance in java



  1. Sub(){
  2. super(1,2); // will work fine
  3. System.out.println("Inside sub class constructor");
  4. }

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu