Explain the difference between 'Is-A' and 'Has-A' relationships.
Last updated
'Is-A' expresses inheritance (a subtype is a kind of its supertype), while 'Has-A' expresses composition (an object contains or uses another object as a field). They answer different design questions: type identity versus ownership of parts.
Is-A (inheritance):
A Dog is-a Animal, so Dog extends Animal and inherits its behavior.
Should hold to the Liskov principle: the subclass must be usable anywhere the parent is expected.
Has-A (composition):
A Car has-a Engine, so Car holds an Engine reference as a member.
Reuses behavior by delegating to the contained object rather than inheriting it.
Guideline: Prefer composition (Has-A) over inheritance (Is-A) when there is no true subtype relationship: it is more flexible and avoids fragile hierarchies.
- https://www.reddit.com/r/developersIndia/comments/1ktgmfk/software_development_interview_preparation/
- https://www.reddit.com/r/programming/comments/1m2ff3j/casey_muratori_the_big_oops_anatomy_of_a/
- https://www.reddit.com/r/SoftwareEngineering/comments/1ijqgpb/is_the_o_in_solid_still_relevant_or_just_a_relic/
- https://www.reddit.com/r/NorthropGrumman/comments/1bt1ug3/monthly_employmentcorporate_questions_and/
- https://www.interviewbit.com/oops-interview-questions/
- https://www.igmguru.com/blog/oops-interview-questions
- https://tryrehearsal.ai/technical-questions/explain-oops-concepts
- https://github.com/Devinterview-io/oop-interview-questions
- https://www.hirist.tech/blog/top-30-c-oops-interview-questions-and-answers/
- https://blog.stackademic.com/dont-just-code-the-15-essential-practices-senior-developers-want-you-to-learn-now-84246fb2f6ed
- https://www.reddit.com/r/ExperiencedDevs/comments/1mpaewh/how_should_i_deal_with_junior_devs_that_debate/
- https://www.scaler.com/topics/oops-concepts-in-cpp/
- https://javatechonline.com/interview-questions-object-oriented-programming/
- https://henko.net/blog/my-favorite-interview-questions/
- https://utkrusht.ai/blog/python-interview-questions
- https://www.digitalocean.com/community/tutorials/oops-interview-questions-answers
- https://www.scribd.com/document/510810151/Fragile-base-class