0% found this document useful (0 votes)
42 views

Introduction To Composition in OOPS

Uploaded by

aamirshahad3321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Introduction To Composition in OOPS

Uploaded by

aamirshahad3321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction to Composition in OOPS

Composition in OOPS is a fundamental concept and describes a class that refers to one or more objects of the other class instance
variables. It drives the design of the application and lets us know how the application should evolve as there are new features
getting added or any requirements change. It is also one of the specialized but restricted forms of aggregation where two entities
are dependent on each other. In Java, Object-Oriented Programming, it implements has-a relationship, which is achieved by using
an instance variable referring to other objects.

Example:

An example of has-a relationship, i.e., Composition in OOPS.

 The class has a teacher

 Zoo has a lion

 The bike has an engine

It is one of the special types of aggregation; restricted aggregation is called composition. This scenario is known as Composition,
when an object contains other objects, and the contained object can’t exist without other objects.

Syntax of Composition in OOPS

There is no particular syntax for composition, but it is pure programming of the concept.

Below is a sample of how composition can be written in Java:

Code:

public class Bike {

private Engine engine; // this shows has – a relationship. (Bike has a Engine)

public Bike() {

this.engine = new Engine();

engine.setCapacity(250L);

For all Composition examples, the user can control the visibility of other objects to other classes but reuse only what is needed. In
addition, it allows back-end class creation when needed.

So by making use of has – a relationship among classes, known as Composition, we are making the program use instance of the
class directly instead of extending it from other classes, similar to inheritance.

 It represents part of the relationship.

 Both entities are dependent on each other, i.e., Bike has – an engine.

 If Bike has – an engine, i.e., Entity1 has an Entity2, which means Entity1 is considered as a whole and Entity2 is part of
Entity1. Hence composing objects can’t exist without other objects.

 If Bike Entity is deleted, the corresponding Entity2 for the Entity1 has to be deleted.
It has a stronger relationship, represents part of a relationship. For e.g., Hotel has a room. The hotel is a whole, and the room is a
part of it. If Hotel is removed from the relationship, then all the corresponding rooms are to be deleted.

Examples of Composition in OOPS

Given below are the examples of Composition in OOPS:

Example #1

Composition in Java OOPS.

Code:

class Car

private String model;

private int wheelCount;

public void catFeatures()

System.out.println("Car Model= "+model + " wheels= " + wheelCount);

public void setModel(String model)

this.model = model;

public void setwheelCount(int wheelCount)

this.wheelCount = wheelCount;

class Maruti extends Car

public void setstartengine()

MarutiEngine me = new MarutiEngine();


me.startengine();

class MarutiEngine

public void startengine()

System.out.println("Maruti Engine starts!");

public void stopengine()

System.out.println("Maruti Engine stops!");

class Demo

public static void main(String[] args)

Maruti m = new Maruti();

m.setModel("Maruti Dzire 2020");

m.setwheelCount(4);

m.catFeatures();

m.setstartengine();

You might also like