Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inheritance – are you supposed to use base classes, or are they just a framework for child classes?

Sorry if this has been asked before. I haven’t found a distinct answer.

I have two classes, motorbike and car. The only difference is number of wheels and number of passengers allowed.

In this case, would I use motorbike as the base class (and have car be the subclass), or create a new ‘vehicle’ base class?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Generally, inheritance is used to model an "isA" relationship.

A car has similar parts to two motorbikes, but a car is not a motorbike.

Both cars and motorbikes, however, are vehicles.

So if it were me, I would use a vehicle class as a base class, and derive both car and motorbike from it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading