What is the superclass method?

The superclass method is used to access the parent class inside a child class. This method has a variety of uses when inheriting parent members.

The words above will be broken down for further understanding later on; however before learning about superclass, you must have an understanding of inheritance in Java so that you can understand the examples in this guide.

What is inheritance and how it is related to superclass?

Inheritance is the process by which one class acquires the propertiesdata members and functionalitiesmethods of another class.

Figure 1: child and parent class relationship.

The diagram above illustrates a connection between the Animal class and the Dog class. The connection between these two is known as inheritance, and the relationship between the two classes is known as Is-a relationship (dog is an animal).

So, from this explanation of inheritance, you may notice that the parent classanimal class acts as the superclass, and the child classdog class as the subclass.

We can’t talk about the superclass method without the super keyword. So, it is time for you to grasp the knowledge of superclass and apply it when needed.

What is the super keyword?

The super keyword refers to the properties and methods of the immediate parent class.

How to use the super keyword

  1. The super keyword is used to access data members of the parent class when the parent and child class have a member with the same name.

  2. The super keyword is used to explicitly call the no-arg and parameterized constructor of the parent class.

  3. The super keyword is used to access the method of parent class when the child class has overridden that method.

Use the super keyword to access parent class method

As discussed earlier the super keyword can be used to access a parent class method. It should be used if the child class contains the same method as the parent class. In other words, the super keyword is used if the method is overridden. You override a method in the parent class by calling the same method in the child class. This process is known as method overriding.

In the provided Java code snippet below, the super keyword gives access to the run method in the Animal class.

class Animal{
void run(){
System.out.println("runs...");}
}
class Dog extends Animal{
void run(){System.out.println("runs faster than goat");}
void bark(){System.out.println("barking...");}
void work(){
super.run();
bark();
}
}
class TestSuper{
public static void main(String args[]){
Dog d=new Dog();
d.work();
}}

How to use the super keyword to access the parent class constructor

You can use the super keyword to access the parent class constructor. Let’s see a basic example:

class Animal{
Animal(){System.out.println("A new animal is created");}
}
class Dog extends Animal{
Dog(){
super();
System.out.println("A new dog is created");
}
}
class SuperClassTest{
public static void main(String args[]){
Dog d=new Dog();
}}

The code snippet above gives a better understanding of how to use the super keyword to access the parent classAnimal class constructor inside the child classDog class.

Time to practice

Now, let’s use the superclass method to access the parent class property or method using a code. Be ready to get your feet wet with Java code and type it out.

class Animal { // Superclass (parent)
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal { // Subclass (child)
public void animalSound() {
super.animalSound(); // Call the superclass method
System.out.println("The dog barks!!!");
}
}
class Main {
public static void main(String args[]) {
Animal myDog = new Dog(); // Create a Dog object
myDog.animalSound(); // Call the method on the Dog object
}
}

Why calling the super method first in the child constructor

You will notice a pattern in the code snippet provided in this guide, where the super method is called first in the child’s constructor. It is important that you notice this pattern.

The reason is that you may want to create an object of the child class, but there may be a couple of propertiesinstance variables or methods that the child class acquires from the parent class through inheritance for which we need to first have those values initialized and created in the parent class object. In this case, super() does it for the child class by calling the constructor of the parent class first. This makes sure that the child’s object creation is consistently created, which is why we need to have it as the first statement in the child’s constructor.

Further learning

What is an implicit and explicit constructor call

Implicit constructor: This type of constructor happens automatically by the compiler (JVM), and is also known as the default constructor. The default constructor is a constructor that has no arguments requested. A default constructor is automatically invoked when you don’t use super().

Explicit constructor: This is the constructor created by you, the programmer.

Thanks for reading.

Free Resources