Java super Keyword

The super keyword in Java is used to refer to the immediate parent class of the current object. It is commonly used in inheritance to access methods and constructors from the parent class that have been overridden or hidden in the subclass.

Table of Contents

  1. Introduction
  2. super Keyword Syntax
  3. Understanding super
  4. Examples
    • Using super to Call Parent Class Methods
    • Using super to Access Parent Class Fields
    • Using super to Call Parent Class Constructors
  5. Real-World Use Case
  6. Conclusion

Introduction

In Java, inheritance allows a class to inherit fields and methods from another class. The super keyword provides a way to access and interact with the parent class’s members, which is useful for code reuse and method overriding.

super Keyword Syntax

The syntax for using the super keyword is as follows:

To call a parent class method:

super.methodName();

To access a parent class field:

super.fieldName;

To call a parent class constructor:

super(parameters);

Understanding super

The super keyword serves three primary purposes:

  1. Calling Parent Class Methods: When a method in a subclass overrides a method in the parent class, super can be used to call the overridden method.
  2. Accessing Parent Class Fields: If a field in a subclass hides a field in the parent class, super can be used to access the hidden field.
  3. Calling Parent Class Constructors: super can be used to call a constructor in the parent class from a constructor in the subclass.

Examples

Using super to Call Parent Class Methods

When a subclass overrides a method in the parent class, super can be used to call the parent class’s version of the method.

Example

class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound.");
    }
}

class Dog extends Animal {
    @Override
    void makeSound() {
        super.makeSound(); // Call the parent class method
        System.out.println("Dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.makeSound();
    }
}

Output:

Animal makes a sound.
Dog barks.

Using super to Access Parent Class Fields

If a field in a subclass hides a field in the parent class, super can be used to access the hidden field.

Example

class Animal {
    String name = "Animal";
}

class Dog extends Animal {
    String name = "Dog";

    void printNames() {
        System.out.println("Name in Dog class: " + name);
        System.out.println("Name in Animal class: " + super.name);
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.printNames();
    }
}

Output:

Name in Dog class: Dog
Name in Animal class: Animal

Using super to Call Parent Class Constructors

The super keyword can be used to call a constructor in the parent class from a constructor in the subclass.

Example

class Animal {
    Animal() {
        System.out.println("Animal constructor called.");
    }
}

class Dog extends Animal {
    Dog() {
        super(); // Call the parent class constructor
        System.out.println("Dog constructor called.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
    }
}

Output:

Animal constructor called.
Dog constructor called.

Real-World Use Case

Extending Functionality

In real-world applications, the super keyword is often used to extend the functionality of an existing class without modifying its code. This is useful in large systems where changes to parent classes must be minimized.

Example

class Employee {
    String name;
    double salary;

    Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    void display() {
        System.out.println("Name: " + name + ", Salary: " + salary);
    }
}

class Manager extends Employee {
    double bonus;

    Manager(String name, double salary, double bonus) {
        super(name, salary); // Call the parent class constructor
        this.bonus = bonus;
    }

    @Override
    void display() {
        super.display(); // Call the parent class method
        System.out.println("Bonus: " + bonus);
    }
}

public class Main {
    public static void main(String[] args) {
        Manager manager = new Manager("Alice", 80000, 5000);
        manager.display();
    }
}

Output:

Name: Alice, Salary: 80000.0
Bonus: 5000.0

Conclusion

The super keyword in Java is used for interacting with parent classes. It allows subclasses to call methods and access fields from their parent classes, as well as invoke parent class constructors. Understanding how to use super effectively is essential for working with inheritance in Java and creating robust, maintainable code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top