What is Composition in Object Oriented Programming ?
There is more to OOP than just Inheritance, Abstraction, Encapsulation, and Polymorphism.
Table of contents
To put simply, you all may know what is Inheritance which is the concept of one class inheriting the properties and methods from another class.
I will use an example here for you further explain this.
Think about a Gaming Mouse and I am going to create this gaming mouse using Mouse class.
public class Mouse {
private String brand;
private String model;
public Mouse(String brand, String model) {
this.brand = brand;
this.model = model;
}
public String getBrand() {
return brand;
}
public String getModel() {
return model;
}
public void click() {
System.out.println("Mouse clicked.");
}
public void scroll() {
System.out.println("Mouse scrolled.");
}
@Override
public String toString() {
return "Mouse [brand=" + brand + ", model=" + model + "]";
}
}
This is the Mouse class which is just the blueprint I am going to use to create my gaming Mouse. ;).
public class GamingMouse extends Mouse {
private boolean rgbLighting;
private int additionalButtons;
public GamingMouse(String brand, String model, boolean rgbLighting, int additionalButtons) {
super(brand, model); // Call the constructor of the parent class
this.rgbLighting = rgbLighting;
this.additionalButtons = additionalButtons;
}
public boolean hasRgbLighting() {
return rgbLighting;
}
public int getAdditionalButtons() {
return additionalButtons;
}
@Override
public void click() {
System.out.println("Gaming mouse clicked with precision.");
}
@Override
public String toString() {
return "GamingMouse [brand=" + getBrand() + ", model=" + getModel() +
", rgbLighting=" + rgbLighting + ", additionalButtons=" + additionalButtons + "]";
}
}
Ta Da !!! I just created my Gaming mouse.
This is Inheritance in other words this will explain a " is a "
relationship.
Composition is a little different from this. Composition describes a " has a / have "
relationship.
Well, now you may wondering what heck is that? isn't it? Okay I will explain this to you using an example I have formed which is a Library system.
Don't worry I will explain everything to you.
First, Let us see what are in the library system are,
A Library
Books
Authors
I will now create the classes for them.
// Author class
public class Author {
private String authorName;
private String nationality;
public Author(String authorName, String nationality) {
this.authorName = authorName;
this.nationality = nationality;
}
public String getAuthorName(){
return authorName;
}
public String getAuthorNationality(){
return nationality;
}
@Override
public String toString(){
return authorName + " (" + nationality + ")";
}
}
Now, pay attention next I am going to create the Book class and this is where I am going to use composition.
// Book class
public class Book {
private String title;
private Author author; // This is what composition.
public Book(String title, Author author){
this.title = title;
this.author = author;
}
public String getTitle(){
return title;
}
public Author getAuthor(){
return author;
}
@Override
public String toString(){
return title + " by " + author.toString();
}
}
As you can see I have commented in the Book class that is where composition came in handy.
What does it mean?
Hey, Book is not a Author right? but book has a author or authors. But for example's sake think one book has a one author ;).
And composition is simple as that.
Let us create the Library class now.
// Library class
import java.util.List;
import java.util.ArrayList;
public class Library{
private String libraryName;
private List<Book> books; // composition
public Library(String libraryName){
this.libraryName = libraryName;
this.books = new ArrayList<>();
}
public void addBook(Book book){
books.add(book);
}
public List<Book> showTheBooksList(){ // Composition.
return books;
}
@Override
public String toString(){
return libraryName + " Library: " + books.toString();
}
}
In the Library class also we have used the Composition.
So, that's what composition is. Hope you got it correct.
Summary
Inheritance allows one class to inherit properties and methods from another class, exemplified by creating a GamingMouse class that extends a Mouse class. Composition, on the other hand, describes a "has a" relationship, illustrated through a Library system where a Book class contains an Author object, and a Library class contains a list of Book objects. This article explains these concepts with practical examples.