by BehindJava
How to resolve the Error return type is incompatible in Java
In this tutorial we are going to learn about, resolving the Error return type is incompatible in Java.
Error : return type is incompatible
- This is because we cannot have two methods in classes that has the same name but different return types.
- A method with the same name as one that already exists in the super class but a different return type cannot be declared in the subclass.
- The subclass may, however, declare a method with the same signature as the superclass. This is what we mean by “Overriding.”
class A {
public void eat() { }
}
class B extends A {
public void eat() { }
}
OR
class A {
public boolean eat() {
// return something...
}
}
class B extends A {
public boolean eat() {
// return something...
}
}
It’s a good idea to mark overwritten methods with the annotation @Override:
class A {
public void eat() { }
}
class B extends A {
@Override
public void eat() { }
}