Explanation About Composition in Java

composition-in-java

Hey Folks, Welcome to Practice House. In this post, I will explain to you about Composition in Java.

Composition in java is one of the best and common practices in Java Programming than Inheritance. Those already know about Inheritance, Congratulations. We want to implements inheritance to perform code reuse and a structure of the IS-A relationship between our classes. With Composition, we will do the same things with a HAS-A Relation.

What is IS-A Relation and What is HAS-A Relation?

This is a nice question. Let me explain in a short way.  When between two classes has an IS-A relationship it will be something like this. A Car is a Vehicle, A Dog is an Animal. So when you will find a relationship like this you will know that it’s called an IS-A Relationship between Classes.

Now let’s talk about HAS-A Relationship. If we use the car then I can say a car has wheels, a car has an engine. A dog has legs,  A dog has a tail.

So in this way, you will identify the relationship between classes and use the best way to implement those relationships.

In this post, we will learn about composition. I told you already that composition is one of the best and common practices in java programming. But you have to make sure that composition is the best suit for your Programs.

Try to use composition. because composition will give a lot of control and accessibility through the program.

composition in java is more powerful than inheritance and extends and is-A relationship model program.

okay now let me give you an example so you can understand more about composition in java.

In this example, we will develop a PC class with its motherboard, casing, and monitor. So in this scenario PC has a motherboard, pc has a casing and pc has a monitor. But Motherboard is not a pc, Casing is not a PC or Monitor also not a PC.

So we are going to write an awesome HAS-A Relationship Model Program.

Example Program for Composition in Java:

This is our project will look like.

composition example program project

In Main.java we will create a PC that has a Motherboard, Case and a Monitor Even Some more our Case has Dimension and Our Monitor has Resolution. Perfect Example of Composition.

Motherboard.java

Case.java

 

Dimension.java

 

Monitor.java

 

Resolution.java

 

PC.java

 

Main.java

 

Sample Output:

Download or Clone From My GitHub:

Download Java Composition Example Project

 

ARE YOU READY FOR A CHALLENGE?

– Create a single room of a house using composition.
– Think about the things that should be included in the room.
– Maybe physical parts of the house and furniture also.
– add at least one method to access an object via a getter and
– then the objects public method as you saw i comment out in the previous example
– then add at least one method to hide the object  e.g. don’t use a getter
– but to access the object used in composition within the main class

If you can solve this A big Congratulations to you. If not Don’t worry See My Solution for this.

If you have any doubt or any question please comments below I would love to hear from you about this. I appreciate you stop by and please inspire me by liking my facebook page or sharing it with your friends or put some constructive comments below. Thank You

2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Md Arifur RahmanRanjith Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Ranjith
Guest
Ranjith

Awesome.. Keep going..

Md Arifur Rahman
Admin

Thanks man…