Composition
- Eclipse: Oxygen
- Java: 1.8Â
 The composition is the core concept of object-oriented programming. The composition is the design technique to implement a has-a relationship between different types of objects/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 the 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 that consists of multiple components and works together as a team to solve a problem. The following components can be simple as well as complex.
In the following list, Screen, RAM, Hard drive, and 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
Processors and graphics cards 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 may have many features. Therefore, we inherit the object and then access the attributes through these objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
public class Laptop { private float screen; private Processor processor; private String ram; private String hardDrive; private GraphicsCard graphicsCard; private String opticalDrive; private String keyboard; public Laptop() { this.screen = 15.6f; this.processor = new Processor(); this.ram = "DDR4"; this.hardDrive = "2TB"; this.graphicsCard = new GraphicsCard(); this.opticalDrive = "MLT layer"; this.keyboard = "backlit"; } public Laptop(float screen, Processor processor, String ram, String hardDrive, GraphicsCard graphicsCard, String opticalDrive, String keyboard) { super(); this.screen = screen; this.processor = processor; this.ram = ram; this.hardDrive = hardDrive; this.graphicsCard = graphicsCard; this.opticalDrive = opticalDrive; this.keyboard = keyboard; } @Override public String toString() { return "Laptop [screen=" + screen + ", processor=" + processor + ", ram=" + ram + ", hardDrive=" + hardDrive + ", graphicsCard=" + graphicsCard + ", opticalDrive=" + opticalDrive + ", keyboard=" + keyboard + "]"; } } |
Output
1 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
public class Laptop { private float screen; private Processor processor; private String ram; private String hardDrive; private GraphicsCard graphicsCard; private String opticalDrive; private String keyboard; public Laptop() { this.screen = 15.6f; this.processor = new Processor(); this.ram = "DDR4"; this.hardDrive = "2TB"; this.graphicsCard = new GraphicsCard(); this.opticalDrive = "MLT layer"; this.keyboard = "backlit"; } public Laptop(float screen, Processor processor, String ram, String hardDrive, GraphicsCard graphicsCard, String opticalDrive, String keyboard) { this.screen = screen; this.processor = processor; this.ram = ram; this.hardDrive = hardDrive; this.graphicsCard = graphicsCard; this.opticalDrive = opticalDrive; this.keyboard = keyboard; } @Override public String toString() { return "Laptop [screen=" + screen + ", processor=" + processor + ", ram=" + ram + ", hardDrive=" + hardDrive + ", graphicsCard=" + graphicsCard + ", opticalDrive=" + opticalDrive + ", keyboard=" + keyboard + "]"; } public Processor getProcessor() { return processor; } public float getScreen() { return screen; } public String getRam() { return ram; } public String getHardDrive() { return hardDrive; } public GraphicsCard getGraphicsCard() { return graphicsCard; } public String getOpticalDrive() { return opticalDrive; } public String getKeyboard() { return keyboard; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
public class GraphicsCard { private String brand; private int series; private String memory; public GraphicsCard() { this.brand = "Nvidia"; this.series = 940; this.memory = "2 GB"; } public GraphicsCard(String brand, int series, String memory) { this.brand = brand; this.series = series; this.memory = memory; } @Override public String toString() { return "GraphicsCard [brand=" + brand + ", series=" + series + ", memory=" + memory + "]"; } } public class Processor { private String brand; private String series; private int generation; private int cores; private int threads; private String cacheMemory; private String frequency; private String minFrequency; private String maxfrequency; public Processor() { this.brand = "intel"; this.series = "i5 7200u"; this.generation = 7; this.cores = 2; this.threads = 4; this.cacheMemory = "3MB"; this.frequency = "2.5Ghz"; this.minFrequency = "2.5Ghz"; this.maxfrequency = "3.1Ghz"; } public Processor(String brand, String series, int generation, int cores, int threads, String cacheMemory, String frequency, String minFrequency, String maxfrequency) { this.brand = brand; this.series = series; this.generation = generation; this.cores = cores; this.threads = threads; this.cacheMemory = cacheMemory; this.frequency = frequency; this.minFrequency = minFrequency; this.maxfrequency = maxfrequency; } @Override public String toString() { return "Processor [brand=" + brand + ", series=" + series + ", generation=" + generation + ", cores=" + cores + ", threads=" + threads + ", cacheMemory=" + cacheMemory + ", frequency=" + frequency + ", minFrequency=" + minFrequency + ", maxfrequency=" + maxfrequency + "]"; } public String getBrand() { return brand; } public String getSeries() { return series; } public int getGeneration() { return generation; } public int getCores() { return cores; } public int getThreads() { return threads; } public String getCacheMemory() { return cacheMemory; } public String getFrequency() { return frequency; } public String getMinFrequency() { return minFrequency; } public String getMaxfrequency() { return maxfrequency; } } public class Hello { public static void main(String[] args) { Laptop lappy = new Laptop(); System.out.println(lappy.getProcessor().getBrand()); } } |
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: Salim Sheikh
a lot of errors in this page.