In previous tutorials, we discussed inheritance in java and types of inheritance in Java. In this guide, we will discuss some of the common errors in inheritance in java along with examples and solutions.
1. Using private
Members in Subclasses
Error: A subclass cannot access private
members of its parent class directly. In the following example, the Child
class tries to access the private variable of Parent
class which resulted in an error.
class Parent {
private int num = 101;
}
class Child extends Parent {
void display() {
System.out.println(num); // Error: num has private access in Parent
}
}
Solution: To avoid this error, you can use two solutions:
- Use
protected
orpublic
access modifiers for variablenum
, this would ensure that the variable is accessible in other classes. Theprotected
variables are only accessible to child classes while public variables are accessible to all classes. I have discussed this here: Access modifiers in Java. - If you wish to keep the
private
access modifier then you can provide getter and setter method in the Parent class to access the variable in child classes.
2. Misunderstanding super
Keyword
Error: Using super
incorrectly to call parent class constructors or methods.
class Parent {
Parent() {
System.out.println("Parent Constructor");
}
}
class Child extends Parent {
Child() {
super(); // Optional, but must be the first statement in the constructor
System.out.println("Child Constructor");
}
}
Solution: Ensure super()
is the first statement in the subclass constructor if used. I have covered this here: Super keyword in Java.
3. Failing to Override Correctly
Error: Forgetting to use the @Override
annotation or mismatching method signatures. I have covered the Override annotation here: Java Override annotation.
class Parent {
void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
@Override
void display() { // Without `@Override`, errors may go unnoticed
System.out.println("Child");
}
}
Solution: Always use @Override
to catch signature mismatches during compilation.
4. Circular Inheritance
Error: A class cannot extend itself or create a circular dependency. This is pretty self explanatory, you should avoid such errors while writing code.
class A extends A {} // Error: cyclic inheritance not allowed
Solution: Redesign the hierarchy to avoid circular dependencies.
5. Constructor Confusion
Error: Forgetting that parent class constructors are not inherited.
class Parent {
Parent(int value) {
System.out.println("Parent: " + value);
}
}
class Child extends Parent {
// Error: Parent class does not have a default constructor
}
Solution: Explicitly call the appropriate parent constructor using super(value)
.
6. Overriding Static Methods
Error: Static methods are not truly overridden; they are hidden. Read more here: static in java.
class Parent {
static void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
static void display() {
System.out.println("Child");
}
}
Parent obj = new Child();
obj.display(); // Outputs "Parent"
Solution: Avoid relying on polymorphism for static methods.
7. Type Casting Errors
Error: Incorrectly casting objects between parent and child types.
Parent p = new Parent();
Child c = (Child) p; // ClassCastException
Solution: Use instanceof
to check the object’s type before casting.
8. Infinite Recursion in Overriding
Error: Calling the same method itself. This would create infinite loop.
class Parent {
void display() {
display(); // Infinite recursion
}
}
Solution: Avoid doing this.
9. Multiple Inheritance via Classes
Error: Java does not support multiple inheritance with classes. I have discussed this here: Multiple inheritance in Java.
class A {}
class B {}
class C extends A, B {} // Error: Multiple inheritance not allowed
Solution: You can use interfaces where you need to use this feature.
10. Failing to Understand the final
Keyword
Error: A final class cannot be inherited. If you try to inherit the final class, you will get an error. I have discussed this in detail: final in Java.
final class Parent {}
class Child extends Parent {} // Error: Parent is final
Solution: Make sure to avoid using final keyword for the classes and methods which you need in other classes.
Leave a Reply