The Artima Developer Community
Sponsored Link

Java Buzz Forum
Super keyword in java inheritance example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Super keyword in java inheritance example Posted: Jul 9, 2016 8:35 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Super keyword in java inheritance example
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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. }

Read: Super keyword in java inheritance example

Topic: What is the difference between byte and char in Java? Previous Topic   Next Topic Topic: Using cost of delay to determine schedule

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use