Handling problems faced with super keyword in OOPS Java

Modified on Tue, 28 Mar, 2023 at 4:00 PM

Error Manifestation: What is the error related to super keyword that users commonly encounter in Java?


Understand the error: The super keyword in Java is used to refer to the parent class and its members. It is often used to call a constructor of the parent class from a subclass. The error related to the super keyword that users commonly encounter in Java is when they try to use the super keyword to call a constructor of the parent class, but they do not provide the correct arguments or do not call it at all. This can result in a compilation error or unexpected behavior at runtime, such as a NullPointerException or a ClassCastException.


Causes: 

  • When the correct arguments are not provided while using the super keyword to call a constructor of the parent class.

  • When the super keyword is not at all used to call a constructor of the parent class.


Check: Can you check where this error is occurring in your code? (Exact file name and line no.)

Check: Check if any of the above causes is applicable for your code? Why do you think the error is being thrown on this line?

Debugging the error: 

How can the user debug the error related to super keyword in Java?


Debug 1:

Check the constructor signature: The first step to debug the error related to the super keyword is to check the signature of the parent class constructor. Ensure that the parameters of the constructor being called match the signature of the constructor in the parent class.


Debug 2:

Call the correct constructor: When calling a constructor of the parent class using the super keyword, ensure that you are calling the correct constructor. If the parent class has multiple constructors, make sure to call the appropriate one.


Debug 3:

Ensure super() is the first statement: When calling the constructor of the parent class using the super keyword, ensure that the super() call is the first statement in the constructor of the subclass.


Code snippet for reference: 

Here's an example code snippet that demonstrates the error related to the super keyword in Java:


public class Animal {

String name;

public Animal(String name) {

this.name = name;

}

}

public class Dog extends Animal {

String breed;

public Dog(String breed) {

this.breed = breed;

}

public void printDetails() {

System.out.println("Name: " + name + ", Breed: " + breed);

}

}

public class Main {

public static void main(String[] args) {

Dog dog = new Dog("Labrador");

dog.printDetails();

}

}



In this code, the Dog class extends the Animal class and has a constructor that takes a breed parameter. However, when we try to print the details of the dog, we get a compilation error because we have not called the constructor of the parent class using the super keyword.


To fix this error, we need to add a call to the constructor of the parent class using the super keyword:


public class Animal {

String name;

public Animal(String name) {

this.name = name;

}

}

public class Dog extends Animal {

String breed;

public Dog(String name, String breed) {

super(name);

this.breed = breed;

}

public void printDetails() {

System.out.println("Name: " + name + ", Breed: " + breed);

}

}

public class Main {

public static void main(String[] args) {

Dog dog = new Dog("Buddy", "Labrador");

dog.printDetails();

}

}


In this updated code, we have added a call to the constructor of the parent class using the super keyword with the "name" parameter. Now, when we run the code, we get the expected output.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article