Super Keyword in Java
Introduction to Super Keyword in Java
The `super` keyword in Java is used to refer to the superclass (parent) object. It is used to call superclass methods, and to access the superclass constructor.
The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name. For example, if the `Animal` class has an `eat()` method, and the `Dog` class inherits from the `Animal` class, the `Dog` class will also have an `eat()` method. However, the `Dog` class's `eat()` method may implement different behavior than the `Animal` class's `eat()` method.
To call the Animal class's eat() method from the Dog class, you can use the `super` keyword. For example, the following code shows how to call the `Animal` class's `eat()` method from the `Dog` class:
public class Dog extends Animal {
@Override
public void eat() {
super.eat();
System.out.println("The dog is eating.");
}
}
The super keyword can also be used to access the superclass constructor. For example, the following code shows how to call the `Animal` class's constructor from the `Dog` class:
public class Dog extends Animal {
public Dog() {
super();
}
}
The super keyword is a powerful tool that can help you write more robust and efficient Java code. It is especially useful for working with classes and inheritance.
Here are some other benefits of using the `super` keyword:
- It can make your code more readable and maintainable by making it clear what code is inherited from the superclass and what code is new in the subclass.
- It can help to prevent errors by preventing you from calling methods or accessing fields that do not exist in the superclass.
- It can be used to implement polymorphism, which is the ability of objects of different types to respond to the same method in different ways.
If you are working with Java, I encourage you to learn more about the `super` keyword and how to use it in your code.
Exercises
Code to demonstrate that a super class constructor will be called when an object is created for the sub class in Java
package javalabdemo;
import java.util.Scanner;
public class javalabclass
{
public static void main(String args[])
{
faculty satish = new faculty();
satish.display_details();
}
}
class person
{
protected String name;
protected String address;
public person() {
// TODO Auto-generated constructor stub
this.name="satish";
this.address="vellore";
}
public void display_details() {
System.out.println(this.name+this.address);
}
}
class faculty extends person
{
}
Code to demonstrate the order of constructor calls in inheritance. When both super class and sub class have constructors, when an object of the sub class is created then the constructor the super class will execute then the sub class constructor will execute.
package javalabdemo;
import java.util.Scanner;
public class javalabclass
{
public static void main(String args[])
{
faculty satish = new faculty();
satish.display_details();
satish.display_faculty();
}
}
class person
{
protected String name;
protected String address;
public person() {
System.out.println("super class constructor called");
this.name="satish";
this.address="vellore";
}
public void display_details() {
System.out.println(this.name+this.address);
}
}
class faculty extends person
{
protected String empid;
public faculty() {
System.out.println("subclass constructor called");
this.empid="123";
}
public void display_faculty()
{
System.out.println(this.empid);
}
}
Code to demonstrate the order of constructor calls with a parameterized constructor in the sub class.
package javalabdemo;
import java.util.Scanner;
public class javalabclass
{
public static void main(String args[])
{
faculty satish = new faculty("123");
satish.display_details();
satish.display_faculty();
}
}
class person
{
protected String name;
protected String address;
public person() {
System.out.println("super class constructor called");
this.name="satish";
this.address="vellore";
}
public void display_details() {
System.out.println(this.name+this.address);
}
}
class faculty extends person
{
protected String empid;
public faculty(String empid) {
System.out.println("subclass constructor called");
this.empid=empid;
}
public void display_faculty()
{
System.out.println(this.empid);
}
}
Code to demonstrate the use of Super method when both super class and sub class has parameterized constructors.
package javalabdemo;
public class javalabclass
{
public static void main(String args[])
{
faculty satish = new faculty("satish","vellore","123");
satish.display_details();
satish.display_faculty();
}
}
class person
{
protected String name;
protected String address;
public person(String name,String address) {
System.out.println("super class constructor called");
this.name=name;
this.address=address;
}
public void display_details() {
System.out.println(this.name+this.address);
}
}
class faculty extends person
{
protected String empid;
public faculty(String name,String address,String empid) {
super(name,address);
System.out.println("subclass constructor called");
this.empid=empid;
}
public void display_faculty()
{
System.out.println(this.empid);
}
}
Code for demonstrating order of constructor calls and the use of Super method in Multi level inheritance.
package javalabdemo;
public class javalabclass
{
public static void main(String args[])
{
professor satish = new professor("satish","vellore","123","116");
satish.display_details();
satish.display_faculty();
satish.display_professor();
}
}
class person
{
protected String name;
protected String address;
public person(String name,String address) {
System.out.println("super class constructor called");
this.name=name;
this.address=address;
}
public void display_details() {
System.out.println(this.name+this.address);
}
}
class faculty extends person
{
protected String empid;
public faculty(String name,String address,String empid) {
super(name,address);
System.out.println("subclass constructor called");
this.empid=empid;
}
public void display_faculty()
{
System.out.println(this.empid);
}
}
class professor extends faculty
{
protected String cabinno;
public professor(String name,String address,String empid, String cabinno) {
super(name,address,empid);
this.cabinno=cabinno;
}
public void display_professor() {
System.out.println(this.cabinno);
}
}
Code for demonstrating the use of Super keyword in invoking the methods of a superclass.
package javalabdemo;
public class javalabclass
{
public static void main(String args[])
{
professor satish = new professor("satish","vellore","123","116");
satish.display_professor();
}
}
class person
{
protected String name;
protected String address;
public person(String name,String address) {
System.out.println("super class constructor called");
this.name=name;
this.address=address;
}
public void display_details() {
System.out.println(this.name+this.address);
}
}
class faculty extends person
{
protected String empid;
public faculty(String name,String address,String empid) {
super(name,address);
System.out.println("subclass constructor called");
this.empid=empid;
}
public void display_faculty()
{
System.out.println(this.empid);
}
}
class professor extends faculty
{
protected String cabinno;
public professor(String name,String address,String empid, String cabinno) {
super(name,address,empid);
this.cabinno=cabinno;
}
public void display_professor() {
//used to call super class methods
super.display_details();
super.display_faculty();
System.out.println(this.cabinno);
}
}
Code for demonstrating the user of Super Keyword to access the variables of the super class.
package javalabdemo;
public class javalabclass
{
public static void main(String args[])
{
professor satish = new professor("satish","vellore","123","116");
satish.display_professor();
}
}
class person
{
protected String name;
protected String address;
public person(String name,String address) {
System.out.println("super class constructor called");
this.name=name;
this.address=address;
}
public void display_details() {
System.out.println(this.name+this.address);
}
}
class faculty extends person
{
protected String empid;
public faculty(String name,String address,String empid) {
super(name,address);
System.out.println("subclass constructor called");
this.empid=empid;
}
public void display_faculty()
{
System.out.println(this.empid);
}
}
class professor extends faculty
{
protected String cabinno;
public professor(String name,String address,String empid, String cabinno) {
super(name,address,empid);
this.cabinno=cabinno;
}
public void display_professor() {
//super used to call super class variables
System.out.println(super.name+super.address+super.empid+this.cabinno);
}
}
Code to demonstrate the use of Super Keyword when the same variable names are used in both super class and sub class.
package javalabdemo;
public class javalabclass
{
public static void main(String args[])
{
professor satish = new professor("satish","vellore","123","116");
satish.display_professor();
}
}
class person
{
protected String name;
protected String address;
public person(String name,String address) {
System.out.println("super class constructor called");
this.name=name;
this.address=address;
}
public void display_details() {
System.out.println(this.name+this.address);
}
}
class faculty extends person
{
protected String empid;
public faculty(String name,String address,String empid) {
super(name,address);
System.out.println("subclass constructor called");
this.empid=empid;
}
public void display_faculty()
{
System.out.println(this.empid);
}
}
class professor extends faculty
{
protected String cabinno;
protected String name;
public professor(String name,String address,String empid, String cabinno) {
super(name,address,empid);
this.cabinno=cabinno;
this.name="test";
}
public void display_professor() {
//super used to call super class variables
System.out.println(this.name+super.name+super.address+super.empid+this.cabinno);
}
}