Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

“Now let’s say ClassA implementation is changed like below, a new method foo() is added.” should be bar not foo.
- Yami
Hi Pankaj, In Inheritance,cant we restrict access to the methods in super class using access modifier private and final keyword or static based on our requirement.So that those methods wont be exposed to outside world. Please clarify. Thanks, Sarathkumar
- Sarathkumar
I am yet to encounter any real example to really prove this. Inheritance is “is-a” relationship. Composition is “has-a” relationship You can interchange them only if you aren’t clear about the class hierarchy. If you quote abstract examples like class A and class B you can write this but otherwise its very difficult to get it wrong. A Human is a Mammal. Can we say Human has a Mammal? Similarly a Car has an Engine, we can never say Car is an Engine. You can confuse this only if you don’t know how the relationship between your classes are? In that case I wouldn’t blindly follow “Prefer Composition vs Inheritance”. I would rather try to find out the correct relationship. Can you please discuss a real example to explain this better?
- Shreyas Majithia
Hi , “Now let’s say ClassA implementation is changed like below, a new method foo() is added” this statement is wrong I believe, here bar is added and not foo Thanks
- Dinesh
Great job explaining it. There are only some formatting issues, the inheritance part is shown as it is a piece of code.
- Malone
Please fix the formatting issue. Everything after the Composition section is shown in code layout.
- John
I think we should not be comparing these two which one is better or not. Both inheritance and composition have different use case. Whatever is explained in the article is syntactical things. We should prefer inheritance when we want to override behavior and re usability both but composition is straight forward example of re usability, or making of small components which together can make a big component.
- Ankit Dixit