View Discussion Improve Article Save Article Like Article View Discussion Improve Article Save Article Like Article The composition is a design technique in java to implement a has-a relationship. Java Inheritance is used for code reuse purposes and the same we can do by using composition. The composition is achieved by using an instance variable that refers to other objects. If an object contains the other object and the contained object cannot exist without the existence of that object, then it is called composition. In more specific words composition is a way of describing reference between two or more classes using instance variable and an instance should be created before it is used. The benefits of using Composition is as follows:
Do remember the certain key points of composition in java which are as follows:
Now let us finally refer to the below image in order to get a faint hint about the aggregation and to better understand how the composition works, let us take an example of the real-time library system.
Implementation: Example
OutputTitle : EffectiveJ Java and Author : Joshua Bloch Title : Thinking in Java and Author : Bruce Eckel Title : Java: The Complete Reference and Author : Herbert Schildt Composition vs Inheritance is one of the frequently asked interview questions. You must have also heard to use Composition over Inheritance. Both composition and inheritance are object-oriented programming concepts. They are not tied up with any specific programming language such as Java. Before we compare composition over inheritance programmatically, let’s have a quick definition of them. CompositionComposition is the design technique in object-oriented programming to implement has-a relationship between objects. Composition in java is achieved by using instance variables of other objects. For example, a person who has a Job is implemented like below in java object-oriented programming. package com.journaldev.composition; public class Job { // variables, methods etc. } package com.journaldev.composition; public class Person { //composition has-a relationship private Job job; //variables, methods, constructors etc. object-orientedInheritanceInheritance is the design technique in object-oriented programming to implement is-a relationship between objects. Inheritance in Java is implemented using the extends keyword. For example, Cat is an Animal relationship in java programming will be implemented like below. package com.journaldev.inheritance; public class Animal { // variables, methods etc. } package com.journaldev.inheritance; public class Cat extends Animal{ }Composition over InheritanceBoth composition and inheritance promote code reuse through different approaches. So which one to choose? How to compare composition vs inheritance. You must have heard that in programming you should favor composition over inheritance. Let’s see some of the reasons that will help you in choosing composition vs inheritance.
That’s all for composition vs inheritance. You have got enough reasons to choose composition over inheritance. Use inheritance only when you are sure that superclass will not be changed, otherwise go for composition. Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest. Sign up |