Showing posts from April, 2021

Composition VS Inheritance in stack's implementation

To prefer composition over inheritance was the choice of many people I happen to learn a lot from, In many cases, it sounded natural and made sense. With composition, you ensure single responsibility and loosely coupled components of your software. However, if we think that inheritance means "Is A" and composition means "Has A", It's very hard in many cases to say that a Doctor has a Person instead of saying a Doctor is a person, I tried but I couldn't do it. Anyway, I'm here for a different reason, I'm here to talk about an example when I had to choose between composition and inheritance and why did I favor one over the other. When I started using dart it originally had no stack implementation, I decided to create it ( Give it a look here ) and I would need to re-use the functionality of the list class in Dart. Inheritance way Let's see, can I say that a stack is a list? maybe, I wouldn't say that in an exam as well as on any other occasion