Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Interface with base class
Message
General information
Forum:
ASP.NET
Category:
Object Oriented Programming
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01416072
Message ID:
01416865
Views:
54
>Hi Viv,
>
>> I don't see why not - AFAICS no strong-typing rules are being broken.
>
>But they are...
>
>Test x = new Test();
>ITest y = (ITest) x;
>x.Parent.MethodOfDerivedClass();
>y.Parent.MethodOfDerivedClass();
>
>The first method call of MethodOfDerivedClass() would need to work since x.Parent is defined as being of type DerivedClass. The second method call would fail, though, because the interface returns the base class. In both cases, however, the code calls exactly the same Parent getter because the interface is implemented implicitly. So, you have the same method but two signatures. If that doesn't violate strong typing, I don't know what else would... The compiler can't do an implicit cast here.

Hi,
Thx. In the clear light of morning I had reached the same conclusion :-}
but realized that I could have used something like this instead:
 public class Test : ITest
    {
        public DerivedClass Parent {get { return new DerivedClass(); }}
        BaseClass ITest.Parent {get { return new DerivedClass(); }}
    }
but in the end it was a better design decision to map the object through a new property anyway....
Best,
Viv
Previous
Reply
Map
View

Click here to load this message in the networking platform