Java
super keyword
object-oriented programming
inheritance
Java programming

super in Java

Object-Oriented Design practice on Codemia

Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.

Practice OOD

Introduction

In Java, super is a reserved keyword that plays a critical role in object-oriented programming (OOP), particularly in the realm of inheritance. It serves as a reference variable that points to the immediate parent class of an object. By using super, developers can access methods and constructors from the parent class when extending a base class, thereby enhancing code functionality and reusability.

Understanding super in Java

Java does not support multiple inheritance directly to avoid ambiguity, but the use of super allows developers to manage inheritance effectively by enabling the following:

  1. Access Parent Class Variables: If child and parent classes have instance variables with the same name, super helps reference the parent class variable explicitly.
  2. Call Parent Class Methods: When a child class overrides methods of the parent class, super can be used to invoke the overridden method from the parent class.
  3. Invoke Parent Class Constructor: super can be used to call constructors from the parent class, ensuring that the parent class is initialized properly before the child class is initialized. This should be the first call within a constructor of the child class.

Examples

Example 1: Accessing Parent Class Variable

java
1class Animal {
2    String name = "Animal";
3}
4
5class Dog extends Animal {
6    String name = "Dog";
7
8    public void printName() {
9        System.out.println(name);  // prints "Dog"
10        System.out.println(super.name);  // prints "Animal"
11    }
12}
13
14public class Main {
15    public static void main(String[] args) {
16        Dog dog = new Dog();
17        dog.printName();
18    }
19}

In this example, the parent class Animal and the child class Dog both have a variable name. Using super.name, the child class can access the name variable from its parent class.

Example 2: Calling Parent Class Method

java
1class Animal {
2    void sound() {
3        System.out.println("Animal makes a sound");
4    }
5}
6
7class Dog extends Animal {
8    void sound() {
9        System.out.println("Dog barks");
10    }
11
12    void makeSound() {
13        super.sound();  // calls sound() of Animal
14        sound();  // calls sound() of Dog
15    }
16}
17
18public class Main {
19    public static void main(String[] args) {
20        Dog dog = new Dog();
21        dog.makeSound();
22    }
23}

Here, super.sound() allows the child class to invoke the overridden sound method in the Animal class.

Example 3: Using super to Call Parent Class Constructor

java
1class Animal {
2    Animal() {
3        System.out.println("Animal is created");
4    }
5}
6
7class Dog extends Animal {
8    Dog() {
9        super();  // calls constructor of Animal
10        System.out.println("Dog is created");
11    }
12}
13
14public class Main {
15    public static void main(String[] args) {
16        Dog dog = new Dog();
17    }
18}

This example demonstrates the use of super() to call the parent class Animal constructor from the child class Dog constructor.

Summary

The following table outlines the key points when using super in Java:

AspectDescription
Access Parent Class Variablesuper.variableName is used to access a variable from the parent class
Call Parent Class Methodsuper.methodName() can invoke a method from the parent class
Invoke Parent Class Constructorsuper() must be the first statement in a child class constructor to call the parent class constructor
Method OverridingAllows overriding of methods while still providing access to the original method via super
Resolves Naming ConflictAvoids confusion by specifying whether the reference is to the parent class or the current class

Additional Details

Limitations of super

  • super only refers to an immediate parent class, not an ancestor further up the inheritance chain.
  • It cannot be used in static methods as it requires an instance of the class to function.

super vs this

  • this refers to the current class instance and can be used to differentiate between instance variables and parameters.
  • super refers to the parent class instance and is pivotal in executing parent class methods and constructors.

By understanding the implementation and nuances of super in Java, developers can effectively manage class hierarchies and reduce redundancy, streamlining their object-oriented programming practices.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Object-Oriented Design practice on Codemia

Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.

Practice OOD

All Rights Reserved.