Difference between Inheritance and Composition

They are absolutely different. Inheritance is an “is-a” relationship. Composition is a “has-a”. You do composition by having an instance of another class C as a field of your class, instead of extending C. A good example where composition would’ve been a lot better than inheritance is java.util.Stack, which currently extends java.util.Vector. This is now … Read more