nus-cs2103-AY2223S2 / forum

12 stars 0 forks source link

Parent class more restrictive than child class? #430

Closed BoAi01 closed 1 year ago

BoAi01 commented 1 year ago

May I know why the parent class is more restrictive than the child class? I thought inheritance makes it more "specialized".

image

Zxun2 commented 1 year ago

Derived classes must be substitutable for their base classes.

If the child class is more restrictive, substituting the parent with the child would result in unexpected behaviors.

You can read more here!

BoAi01 commented 1 year ago

Alright, thanks. I would really think it depends on how we define "restrictive" though. If A can substitute B (A is a sub-class of B) means A is less restrictive, this statement makes sense, yes.

Thanks!

damithc commented 1 year ago

Yup, don't equate restrictive with specialized. The child classes can be more specialized without being more restrictive than the parent class.