Composition

  • Eclipse: Oxygen
  • Java: 1.8 

     The composition is the core concept of object-oriented programming. The composition is the design technique to implement has-a relationship between different types of object/classes. We can use Java inheritance or object composition in Java for code reuse.

    Difference between inheritance and composition?

    In Java, Inheritance is suitable only when classes are in a relationship. For example Car, Truck Bike are a vehicle and all these vehicles have common features of vehicle class (Superclass). But to represent engine relationship, we have an alternative to inheritance known as composition.

    In inheritance, we can derive some functionality to a subclass of the parent class, in the composition a class reuses the functionality by creating a reference to the object of the class.

    For example, A laptop is a composite object containing other objects such as Screen, processor, Ram, Hard drive, Graphics card, Optical drive, and Keyboard. In other words, the laptop object shares a HAS-A relationship with other objects. The laptop is a composition which consists of multiple components and works together as a team to solve a problem. Following components can be simple as well as complex.

In the following list, Screen, RAM, Hard drive, optical drive Keyboard are simple composition.

  • Screen -> Full HD/HD
  • Ram-> DDR1/DDR2/DDR3/DDR4
  • Hard drive-> 500 GB/1 TB/ 2TB
  • Optical drive-> Single layer/multilayer
  • Keyboard-> backlit/standard

Processor and graphics card are complex components. There are multiple things involved.

Processor

  • Brand
  • Series
  • Generation
  • No of Cores
  • No of threads
  • Cache memory
  • Frequency

Graphics card

  • Brand
  • Series
  • Memory

Composition setting up

In inheritance, we deal with the attributes of other classes. Composition studies have a relationship in which we ‘inherit’ or use objects from other classes.

In the following example, Laptop HAS-A Processor which may have many features. Therefore, we inherit the object and then access the attributes through these objects.

Output

Laptop [screen=15.6, processor=Processor [brand=intel, series=i5 7200u, generation=7, cores=2, threads=4, cacheMemory=3MB, frequency=2.5Ghz, minFrequency=2.5Ghz, maxfrequency=3.1Ghz], ram=DDR4, hardDrive=2TB, graphicsCard=GraphicsCard [brand=Nvidia, series=940, memory=2 GB], opticalDrive=MLT layer, keyboard=backlit]

Working with composition

As we discussed, the classes inherit the objects of other classes. Hence to access the attributes we need to create the getters to return the object of the classes by the virtue of which we can access the required attribute.

Output

intel

Importance:

  • By using composition, we can control the visibility of another object for customer classes and reuse only what we need.
  • The composition allows the creation of backend classes when necessary.

Contributed by: Poonam Tomar


VIEW COURSES

Buy me a coffee!

 

PayTM QR (INDIA)

  • Get video tutorials
  • Project files set
  • Get Certified

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu