Reputation: 5281
When a child class inherits from both a parent class and an interface, why can the child class not access a parent class method?
Consider the following, where I can't access the DoSomething()
method.
class Program
{
static void Main(string[] args)
{
IMyInterface myClass = null;
myClass = new ChildClass();
// this returns error
myClass.DoSomething();
}
}
internal class ParentClass
{
public ParentClass() { }
public void DoSomething() { }
}
internal class ChildClass : ParentClass, IMyInterface
{
public string MyProperty { get; set; }
public ChildClass() : base() { }
}
internal interface IMyInterface
{
string MyProperty { get; set; }
}
I've looked through SO discussions here and here, but they seem to focus on how a member is hidden using new, override, and virtual keywords... sorry, but I can't figure out how that applies to this situation. Also, I've browsed through the MSDN API reference on interfaces here and here, with no luck.
Upvotes: 0
Views: 255
Reputation: 1249
The best way to understand this is to know the difference between an interface and a parent/child class.
An interface is a contract that can exist on any class regardless of it's inheritance chain. You could put that interface on a class that doesn't inherit from ParentClass, and all that class has to fulfill is what is in your interface (in your case, the MyProperty property). If you added DoSomething() to the interface, this class would then also be required to have that method.
A subclass (child class) inheriting from a parent class has established a relationship. The parent class shares it's non-private methods/properties/member subset with it's child class. Therefore, you can cast a child class to it's parent class and retain accessibility to those properties.
Upvotes: 0
Reputation: 218818
Because the type of myClass
is IMyInterface
:
IMyInterface myClass = null;
and IMyInterface
doesn't have a DoSomething()
method:
internal interface IMyInterface
{
string MyProperty { get; set; }
}
However, with polymorphism the type cal also be ParentClass
or ChildClass
. So you can use the method by morphing the type:
(myClass as ChildClass).DoSomething();
As with any time you cast or morph the type of an object, be careful for null
s. If the type can't be converted then myClass as ChildClass
would be null
, so the above would result in a NullReferenceException
.
Upvotes: 0
Reputation: 8610
The problem here is specifically to do with the declaration of the variable in your Main method.
IMyInterface myClass = null;
myClass = new ChildClass();
// this returns error
myClass.DoSomething();
Taking lines in isolation, we can reduce it to just this.
IMyInterface myClass = null;
// BLAH BLAH myClass gets initialized somehow, we don't know/care how.
myClass.DoSomething();
So at that point, we only know we have an initialized object of interface IMyInterface
. In other words, taking that line alone, we don't know that it's a ChildClass
. The only known method of IMyInterface
is MyProperty
, so that's the only thing we know is available to us.
You could fix this by declaring myClass specifically as a ChildClass
instance. You can even return this variable in a method that expects to return an IMyInterface
type.
Upvotes: 4
Reputation: 3631
DoSomething()
is from ParentClass
, and you are using an IMyInterface
reference.
To use this method, you need to do a cast:
((ChildClass) myClass).DoSomething();
or
((ParentClass) myClass).DoSomething();
Upvotes: 1
Reputation: 79441
This does not work for a very straightforward reason:
IMyInterface
does not have a DoSomething
method.
If you modify your interface as follows, your code will work.
internal interface IMyInterface
{
string MyProperty { get; set; }
void DoSomething();
}
Upvotes: 2