Question

I have an abstract class having an abstract method:

public abstract SomeClass Do(SomeClass o1, SomeClass o2);

Then when I implement in a concrete class which extends that abstract class which has the method from before it I get an error:

@Override 
public AClass Do(AClass o1, AClass o2) //AClass extends Someclass
{
//
}

Why do I get the error since AClass is a subtype of SomeClass?

The error is: The method ... must override or implement a super-type method

Was it helpful?

Solution

You get an error because it doesn't support the same interface. Imagine a caller like this:

AbstractClass x = new ImplementingClass();
x.Do(new SomeClass(), new SomeClass());

That should work - it's just using the AbstractClass abstract method, right? But the values you're passing in aren't AClass references, they're just SomeClass references.

Your return type is okay, but the parameters you're declaring are "narrower" than the original API, so your method can't handle all the calls that the original method could, and so can't override it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top