Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

Java

Learn step by step



super keyword

The super keyword enables a subclass to call the methods and fields of its superclass. It is not an instance of the superclass object but a way to tell the compiler which methods or fields to reference. The effect is the same as if the subclass is calling one of its own methods.

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

Name from subclass : Apple
Name from subclass : Fruit
Apple !
Fruit !
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

Super class default constructor !
Sub class default constructor !
Super class default constructor !
Sub class parameterized constructor : argument1
Super class parameterized constructor : arg1 and arg2
Sub class parameterized constructor : argument1 and argument2
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 !");
}
}
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

Fruit's default constructor !
Apple's default constructor !
AppleChild's default constructor !


Related Videos