using Intercase and inheritance

T

Tony Johansson

Hello!

Assume I have this IComparable interface and
I implement this IComparable in my class called MyTop see below.
If I now inherit a new class called MySub from MyTop this new class
is not obliged to implement this IComparable interface isn't that strange.

Wouldn't it be more correct to let the inherit class in this case mySub to
be obliged to implement the IComparable
interface.

I'm I totaly wrong here??

I have made a test this MySub just inherit this CompareTo method.

public interface IComparable
{
int CompareTo(IComparable comp);
}
class MyTop : IComparable
{
//Here I implement the CompareTo method
}

class MySub : MyTop
{
....
}

//Tony
 
M

Marc Gravell

It already *does* implement it - it just uses the implementation
already declared by MyTop.

MySub could choose to re-implement the interface, but that often gets
confusing. A better option is to make the implementation (in MyTop)
virtual so that MySub can override the behaviour. Of course, you may
also wish to consider the impact of IComparable<MySub> versus
IComparable<MyTop> (using the generic form).

The only way to *force* a subclass to implement something is to leave
a method abstract. But then your base-class must be abstract.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top