Learn all about Super Keyword in Java with example i.e. where super keyword can be used in java programs. For example, invoking constructor, method and variables etc. of base class from derived class in inheritance relationship.

Super keyword can be used for following concepts.

  1. Calling super class constructor from derived class.
  2. Calling immediate parent class / super class method.
  3. Initialize a base class variable from derived class.

NOTE:


Base class / super class/ parent class all are terminology for base class. Derived class/ child class / sub class all are terminology for the derived class.

PREREQUISITE: To understand the super keyword you must be familiar with following java concepts

1)Call base class constructor from derived class using super keyword in java

We use super keyword in java program to call base class constructor for derived class. You can see the example how super keyword has been used in derived class constructor to invoke base class constructor.

Important point: If the base class constructor has no parameter, then we don’t need to call super() in derived class constructor and compiler will call it implicitly.

However, if the base class constructor has parameters then you must use super keyword with parameter.

A) Calling base class constructor with no parameter from derived class using super

Java code:

/*-------------------------------------------------
 * Example - Calling base class constructor
 */

class Parent {

	// Constructor
	public Parent() {
		System.out.println("Parent class constructor");
	}
}

class Child extends Parent {

	public Child() {

		// Super call base class constructor

		// This is option for empty parameter
		// Even though you don't write it base class
		// constructor will be automatically called.
		super();

		System.out.println("Child class constructor");
	}
}

public class Sample {

	public static void main(String[] args) {

		// Create the object of child
		// base class constructor will also be called
		Child ch = new Child();

	}

}

Output:
Parent class constructor
Child class constructor

B) Calling parameterized constructor of base class from derived class using super

/*-------------------------------------------------
 * Example - Calling base class constructor
 */

class Parent {	
	// Constructor
	public Parent(int age) {
	
		System.out.println("Parent class constructor" + "Age :"+age);
	}
}

class Child extends Parent {

	public Child() {

		//invoke base class constructor with parameter
		//ass the parent age
		super(40);

		System.out.println("Child class constructor");
	}
}

public class Sample {

	public static void main(String[] args) {

		// Create the object of child
		// base class constructor will also be called
		Child ch = new Child();

	}

}


Output:
Parent class constructor Age :40
Child class constructor

2)Call Base class method from Derived class using Super Keyword

You can call a base class public method from a derived class using super keyword in java programs.

For example, consider the Parent and Child class both have mobile () methods. When you create object of child class and call mobile () method, then child class mobile method will be called. Because, Child class extends parent class and has overridden mobile method. You can refer method overriding in java programs.

So, If we want to call parent class mobile method from child class method, then we need to call the parent’s class method using super keyword.

In below java code, mobile method of parent class has been called using super keyword in child class mobile method.

/*-------------------------------------------------
 * Example - Calling base class method from derived class method.
 */

class Parent {

	public void mobile() {
		System.out.println("Parent's mobile");
	}
}

class Child extends Parent {

	public void mobile() {

		// Call parent's mobile() method		
		super.mobile();

		System.out.println("Child's mobile");

	}
}

public class Sample {

	public static void main(String[] args) {

		// Create the object of child
		Child ch = new Child();
		ch.mobile();// class child class method

	}

}

Output:
Parent’s mobile
Child’s mobile

3) Access base class public variables from derived class using super keyword

Lets say we a common field e.g. public int account is in both class parent and child. Child class extends base class. So base call variable is also accessible to child class. But, when you print the account variable in child class, then child class variable will be printed not base class.

So, using super keyword, we can refer base class variable from child class method.

/*-------------------------------------------------
 * Example - Access base class variable from derived class
 */

class Parent {

	public int account = 10000;

	public void mobile() {
	}
}

class Child extends Parent {

	public int account = 5000;

	public void account() {

		// Show parent account
		System.out.println("Parent's Account: " + super.account);

		// show child account
		System.out.println("Child Account: " + account);

	}
}

public class Sample {

	public static void main(String[] args) {

		// Create the object of child
		Child ch = new Child();
		ch.account();

	}

}

Output:
Parent’s Account: 10000
Child Account: 5000

Related Posts