I have an interface and an abstract class inheriting this interface.
public interface ISomeInterface
{
int Method()
{
return 1;
}
}
public abstract class SomeAbstractClass : ISomeInterface {}
Now I want to implement a class inhereting SomeAbstractClass
that will also implement int Method()
.
public class SomeClass : SomeAbstractClass
{
public int Method()
{
return 2;
}
}
However when calling Method()
on a casted SomeClass
object to ISomeInterface
it'll display 1.
ISomeInterface someClass = new SomeClass();
Console.WriteLine($"{someClass.Method()}"); // displays 1
But if I add interface to SomeClass
public class SomeClass : SomeAbstractClass, ISomeInterface
{
public int Method()
{
return 2;
}
}
It'll display 2.
Why is that the case? Is there any way to declare/implement Method()
by just inhereting from SomeAbstractClass
but without the need to write , ISomeInterface
too?
I also don't want to modify the SomeAbstractClass
if possible.
I tried searching for the explanation online, but it's difficult to explain this problem in a simple sentence. I've tried to read more about default interface methods, but learned nothing meaningful.