Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Saturday, 19 October 2013

Super keyword in Java - Example and Explanation

In our previous discussions we came across 'What is method overloading in Java'. In this particular blog we will see 'What is super keyword in java'. We will walk through all possible scenarios to use 'super' keyword in java programming.

In short we can say than, super is used to serve following two objectives:
1) Accessing super class members (Methods and Variables).
2) Calling super class Constructor.


1) Using 'super' to access superclass members

In Java 'super' keyword can be used to refer superclass instance in a subclass. In the code below we have a superclass 'Fruit' and a subclass 'Apple'. These two classes have member variable 'name' in them. And a method 'getClassName()' that is being overriden by 'Apple'. In normal condition if we call 'getClassName()' or try to access 'name' with apple instance, we will get members of 'Apple' called. To call superclass(Fruit) members withinn a subclass(Apple) 'super' keyword is used.
package com.beingjavaguys.core;
public class Fruit {
 String name = "Fruit";
 
 public void getClassName(){
  System.out.println("Fruit !");
 }
}



public class Apple extends Fruit {
 String name = "Apple";

 @Override
 public void getClassName() {
  System.out.println("Apple !");
 }

 void printName() {
  /*
   * this will print value of 'name' from subclass(Apple)
   */
  System.out.println("Name from subclass : " + name);
  /*
   * this will print value of 'name' from superclass(Fruit)
   */
  System.out.println("Name from subclass : " + super.name);

  /*
   * invoke Apple class's 'getClassName()' method, simply
   */
  getClassName();

  /*
   * invoke (superclass's)Fruit class's 'getClassName()' overriden method,
   * using 'super' class
   */
  super.getClassName();
 }

}



public class Implementation {
 public static void main(String args[]){
  Apple apple = new Apple();
  apple.printName();
 }
}


Output:

In console we can see that without 'super' members of subclass are called, while using 'super' members of superclass can be called.


2) Using 'super' to call superclass constructor

Another purpose of using 'super' is to call superclass constructor from a subclass. Although every time a parameterized or non-parameterized constructor of a subclass is created, a default constructor of superclass in called by default implicitly.
Here the thing to be considered is that, implicitly a default constructor of superclass is called not a parameterized one. To call a parameterized constructor of a superclass we must use 'super(parameters..)' with matching parameters.
package com.beingjavaguys.core;
public class Fruit {
 Fruit(){
  System.out.println("Super class default constructor !");
 }
 
 Fruit(String arg1){
  System.out.println("Super class parameterized constructor : "+arg1);
 }
 
 Fruit(String arg1, String arg2){
  System.out.println("Super class parameterized constructor : "+arg1+" and "+arg2);
 }
 
 
}


package com.beingjavaguys.core;
public class Apple extends Fruit {

 /*
  * a default constructor will be called no matters of you used super() as
  * first line or not here
  */
 Apple() {
  System.out.println("Sub class default constructor !");
 }

 /*
  * a default constructor will be called implicitly no matters if the
  * subclass constructor is parameterized first line or not here
  */
 Apple(String arg1) {
  System.out.println("Sub class parameterized constructor : " + arg1);
 }

 /*
  * to call a parameterized constructor of superclass you must write super() with matching arguments
  * in absense of super(para..) only a default constructor of superclass will be called
  */
 Apple(String arg1, String arg2) {
  super("arg", "arg2");
  System.out.println("Sub class parameterized constructor : " + arg1
    + " and " + arg2);
 }
}




package com.beingjavaguys.core;
public class Implementation {
 public static void main(String args[]){
  Apple apple = new Apple();
  Apple apple1 = new Apple("argument1");
  Apple apple2 = new Apple("argument1","argument2");
 }
}

Output:

In console we can see that, implicitly a default constructor of superclass is called not a parameterized one. To call a parameterized constructor of a superclass we must use 'super(parameters..)' with matching parameters.



2.1) What if a class don't extends another class, still uses 'super' in his constructor.

If a class dont extends another class and still uses a super() call, this is perfectly ok. No error will be shown because every independent class extends 'Object' class by default. And in this case a dafult constructor of 'Object' class is being called.
package com.beingjavaguys.core;
public class Apple {
 Apple() {
  super();
  System.out.println("Sub class default constructor !");
 }
}




2.2) What if there is a chain of extended classes and 'super' keyword is used

Now the question comes, what if there is a chain of extending classes and a subclass in the end of hierarchy calls a 'super()'. Yes, this is perfectly true in this case the default constructors of all classes in the hierarchy will be called implicitly.
package com.beingjavaguys.core;
public class Fruit {
 Fruit(){
  System.out.println("Fruit's default constructor !");
 }
}

package com.beingjavaguys.core;
public class Apple extends Fruit {
 Apple() {
  super();
  System.out.println("Apple's default constructor !");
 }
}

package com.beingjavaguys.core;
public class AppleChild extends Apple {
 AppleChild() {
  super();
  System.out.println("AppleChild's default constructor !");
 }
}

package com.beingjavaguys.core;
public class Implementation {
 public static void main(String args[]){
  AppleChild appleChild = new AppleChild();
 }
}


Output:

In console we can see that, the default constructor of all classes in the hierarchy are called one by one to the top of hierarchy.


Here we are done with 'What is Super keyword in Java - Example and Explanation '. In our upcoming blogs we will see more about Java Programming and other opensource technologies.








Thanks for reading !
Being Java Guys Team




5 comments:

  1. In second example you have not used Super() in both Apple() and Apple(one parameter) constructor, then how it prints 'Super class default constructor !' and 'Super class Parametraised constructor !'

    ReplyDelete
  2. We dont need to use super key word to invoke default constructor

    ReplyDelete
  3. By the way , Nagesh, you have done a wonderful job by explaining this in a perfect way. Thank you very much.

    ReplyDelete
  4. This is quite interesting! And I am very excited to learn from your blog.

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft