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.
Inheritance is the process by which one class acquires the
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
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.
The super
keyword refers to the properties and methods of the immediate parent class.
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.
The super
keyword is used to explicitly call the no-arg and parameterized constructor of the parent class.
The super
keyword is used to access the method of parent class when the child class has overridden that 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();}}
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
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 methodSystem.out.println("The dog barks!!!");}}class Main {public static void main(String args[]) {Animal myDog = new Dog(); // Create a Dog objectmyDog.animalSound(); // Call the method on the Dog object}}
super
method first in the child constructorYou 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 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.
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.