A composition in Java between two objects associated with each other exists when there is a strong relationship between one class and another.
Composition is deal with has
relationship.
package anote;
public class Main {
public static void main(String[] args) {
Dimensions dimensions = new Dimensions(20, 20, 7);
Case theCase = new Case("220B", "Samsung", "240", dimensions);
Monitor theMonitor = new Monitor("27inch ", "LG", 27, new Resolution(2540, 1440));
Motherboard theMotherboard = new Motherboard("BJ-200", "Samsung", 4, 6, "v2.44");
PC thePC = new PC(theCase, theMonitor, theMotherboard);
thePC.powerUp();
// So, the PC has a monitor, a motherboard, and a case.
}
}
class PC {
private Case theCase;
private Monitor monitor;
private Motherboard motherboard;
public PC(Case theCase, Monitor monitor, Motherboard motherboard) {
this.theCase = theCase;
this.monitor = monitor;
this.motherboard = motherboard;
}
public void powerUp() {
theCase.pressPowerButton();
drawLogo();
}
private void drawLogo() {
monitor.drawPixelAt(1000, 50, "red");
}
}
class Monitor {
// Monitor has a Resolution
private String model;
private String manufacturer;
private int size;
private Resolution nativaResolution;
public String getModel() {
return this.model;
}
public String getManufacturer() {
return this.manufacturer;
}
public int getSize() {
return this.size;
}
public Resolution getNativaResolution() {
return this.nativaResolution;
}
public Monitor(String model, String manufacturer, int size, Resolution nativaResolution) {
this.model = model;
this.manufacturer = manufacturer;
this.size = size;
this.nativaResolution = nativaResolution;
}
public void drawPixelAt(int x, int y, String color) {
System.out.println("Drawing pixel at " + x + "," + y + " in color " + color);
}
}
class Motherboard {
private String model;
private String manufacturer;
private int ramSlots;
private int cardSlots;
private String bios;
public void loadProgram(String programName) {
System.out.println("Program " + programName + " is now loading...");
}
public String getModel() {
return this.model;
}
public String getManufacturer() {
return this.manufacturer;
}
public int getRamSlots() {
return this.ramSlots;
}
public int getCardSlots() {
return this.cardSlots;
}
public String getBios() {
return this.bios;
}
public Motherboard(String model, String manufacturer, int ramSlots, int cardSlots, String bios) {
this.model = model;
this.manufacturer = manufacturer;
this.ramSlots = ramSlots;
this.cardSlots = cardSlots;
this.bios = bios;
}
}
class Case {
// Case has a dimensions
private String model;
private String manufacturer;
private String powerSupply;
private Dimensions dimensions;
public Case(String model, String manufacturer, String powerSupply, Dimensions dimensions) {
this.model = model;
this.manufacturer = manufacturer;
this.powerSupply = powerSupply;
this.dimensions = dimensions;
}
public void pressPowerButton() {
System.out.println("Power button pressed");
}
public String getModel() {
return model;
}
public String getManufacturer() {
return manufacturer;
}
public String getPowerSupply() {
return powerSupply;
}
public Dimensions getDimensions() {
return dimensions;
}
}
class Dimensions {
private int width;
private int height;
private int depth;
public Dimensions(int width, int height, int depth) {
this.width = width;
this.height = height;
this.depth = depth;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getDepth() {
return depth;
}
}
class Resolution {
private int width;
private int height;
public int getWidth() {
return this.width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return this.height;
}
public void setHeight(int height) {
this.height = height;
}
public Resolution(int width, int height) {
this.width = width;
this.height = height;
}
}
Power button pressed
Drawing pixel at 1000,50 in color red