Inheritance is-a and has-a relationship | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Inheritance is-a and has-a relationship

If a define base class vehicle and inherit new class car from base class vehicle, so object of class car is object of vehicle. It implies that car is a vehicle but vehicle is not (only) a car. It means if Vehicle vehicle; Car car; then car = vehicle is a valid statement or assignment, but vehicle = car is not a valid assignment. Am I right? Please give your opinion, class.

10th Dec 2022, 9:59 AM
Oliver Pasaribu
Oliver Pasaribu - avatar
1 Answer
+ 2
'has-a' relationship (if that was the term), is more closely related to composition. Like a vehicle is composed of chassis, engine etc. 'is-a' relationship is like what you wrote, car is-a vehicle, but a vehicle is not limited to, just a car. But I'm not sure the `=` operator can be used to verify whether or not `car` inherits, or was extended from `vehicle`.
10th Dec 2022, 3:25 PM
Ipang