on Leave a Comment

Super Keyword in Java With Example

The super keyword is used to differentiate the members for superclass from the members of base class. Actually, the super keyword in java is a reference variable which refers immediate parent class of an object.

Use of super keyword in java

- For referring variables of immediate parent class.
- For referring methods of immediate parent class.
- For referring constructors of immediate parent class.

Why super keyword?

When a subclass inherits properties of super class, there is a possibility that some properties of subclass is similar to super class, this creates ambiguity for JVM. To differentiate the properties of base class from subclass, super keyword is used.

Super Keyword for Variables in Java

If any variable of both subclass and superclass having same features, then super keyword can be used to refer superclass variable to avoid ambiguity.

Example of Super Keyword for Variables in Java

class class1
{
 int a = 10;
}
class class2 extends class1
{
 int a = 20;
 void show()
 {
  System.out.println("Value of a = "+super.a); //prints super class variable
  System.out.println("Value of a = "+a); //prints subclass variable
 }
}
class SuperKeyword
{
 public static void main(String[] args) {
  class2 obj = new class2();
  obj.show();
 }
}

Output:

Value of a = 10
Value of a = 20

Super Keyword for Methods in Java

The super keyword can be used to refer superclass methods. The super keyword is very used in the case of method overriding.

Example of Super Keyword for Methods in Java

class class1
{
 void show()
 {
  System.out.println("Superclass method");
 }
}
class class2 extends class1
{
 void show()
 {
  super.show(); 
  System.out.println("Subclass method");
 }
}
class SuperKeyword
{
 public static void main(String[] args) {
  class2 obj = new class2();
  obj.show();
 }
}

Output:

Superclass method
Subclass method

Super Keyword for Constructors in Java

The super keyword can be used to invoke constructors of superclass. A superclass can have default constructor and parametrized constructor. If we do not create any constructor in class, then compiler automatically creates default constructor. We can also call default constructor in subclass by super() statement. Compiler automatically writes super() statement in subclass constructor as a first statement.

Invoking super class default constructor by super keyword

class class1
{
 class1()
 {
  System.out.println("Superclass default constructor");
 }
}
class class2 extends class1
{
 class2()
 {
  super();
  System.out.println("Subclass default constructor");
 }
}
class SuperKeyword
{
 public static void main(String[] args) {
  class2 obj = new class2();
 }
}

Output:

Superclass default constructor
Subclass default constructor

Invoking super class parameterized constructor by super keyword

class class1
{
 class1(int a, int b)
 {
  System.out.println("Superclass parameterized constructor");
 }
}
class class2 extends class1
{
 class2()
 {
  super(1, 2);
  System.out.println("Subclass default constructor");
 }
}
class SuperKeyword
{
 public static void main(String[] args) {
  class2 obj = new class2();
 }
}

Output:

Superclass parameterized constructor
Subclass default constructor


0 comments:

Post a Comment