Hello fellow programmers, I am reading a book on C# and the author is comparing
ID: 643372 • Letter: H
Question
Hello fellow programmers,
I am reading a book on C# and the author is comparing Abstract classes and Interfaces. He claims that if you have the following "abstract class:"
Then you cannot do this:
This, I understand. However he claims that because of this inability to do multiple inheritance that you should use an interface for CloneableType, like so:
My question is, isn't this somewhat misleading, because you can create an abstract class which is "above" class Car with the method Clone, then have Car inherit that class and then Minivan will inherit Car with all these methods, CloneAble class -> Car class -> Minivan Class.
What do you think? Thanks.
Explanation / Answer
The difference between the abstract classes and interfaces in your case could allow the developer to well-define the pattern of your design with abstract class create "is a" relationship and interface create "can do" relationship.
Put it this way, if you have a Toy car and it would be not make sense to make them "is a" relationship with Minivan since you can use interface to exhibit toy car specific behaviour like water resistance tolerance.
So you can define like:
public Interface IToyCarResistance
{
int ToleranceLevel;
}
public class ToyCar : Car , IToyCarResistance
{
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.