1

I have a question regarding how a child class inherits from another child class which inherits from the base class. Consider the following: Triangle inherits from Shape, and RightTriangle inherits from Triangle. I have an image below to describe the scenario.

Can it be possible for RightTriangle to inherit directly from Shape (Given that it is also a shape)?

Imgur

6
  • What do you mean by inherit directly ? Commented Jun 10, 2016 at 14:23
  • 3
    I'm not sure I understand your goal ; it is possible to inherit directly, sure, but you'd be lacking the triangle layer which would be confusing. And this would also make you unable to use triangle-specific properties or methods, which is probably something you'd need in a right-triangle. Possible, yes, practical/advised, definitely no. Also, this is probably not a question for SO, but more likely for programmers.stackechange.com Commented Jun 10, 2016 at 14:24
  • 1
    There does not seem to be anything in this question that justifies a C# tag. Any answer that applies to C# would apply to another language with a similar inheritance model. Commented Jun 10, 2016 at 14:25
  • 4
    RightTriangle already is a Shape, as it inherits from Triangle which inherits from Shape. Keep in mind that C# doesn't allow multiple inheritance, so that chain inheritance is the only way to accomplish a similar behavior Commented Jun 10, 2016 at 14:26
  • 1
    @RubensFarias you can also use interfaces to separate shared semantics from inheritance. Commented Jun 10, 2016 at 14:36

2 Answers 2

2

"It depends".

If the types are immutable, where "modifying" operations return a new instance then this could work.

But if there are mutating operations then it will not.

Consider:

Rectangle rect = new Square(10); // Sides of length...
rect.SetWidth(20);

Does that mean rect is no longer a square, does it throw, or does it set the height as well? All three results are bad (and violate Liskov).

In the immutable case rect.SetWidth would return a new instance, each subtype would need to be written with logic to handle switching types when required (eg. in the above rect.SetWidth(10) could return a Square because the sides remain the same length).

While geometrically the hierarchy works, that is because in geometry all operations create new shapes and you then look at the properties of the new shape to classify it. In a statically typed language with mutable objects neither of those restrictions applies.

Sign up to request clarification or add additional context in comments.

Comments

1

Part I:
Can it be possible for RightTriangle to inherit directly from Shape
Yes, if RightTriangle does not inherit from Traingle
No, If RightTriangle inherit from Traingle --> C# Does not support multi inheritance -> compile error

Part II:
Given that it is also a shape
Same here if RightTriangle inherit from Shape then you can cast it as a Shape.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.