In Java, Composition, Inheritance, and Delegation are three fundamental concepts used in object-oriented programming, each serving distinct purposes in structuring classes and their relationships.
Composition
Composition involves creating complex objects by combining simpler ones, forming a “has-a” relationship. In Java, this is achieved by including an instance of another class within a class. For example, a Car class may contain instances of Engine, Wheel, and Chassis classes. This approach promotes code reuse, modularity, and flexibility by allowing changes to the composed classes independently.
class Engine { /* Engine properties and methods */ }
class Wheel { /* Wheel properties and methods */ }
class Car {
private Engine engine;
private Wheel[] wheels;
// Other properties and methods
}
Code language: PHP (php)
Here’s an example that demonstrates composition:
class Instrument { private String name; public Instrument(String name) { this.name = name; } public void play() { System.out.println(name + " is played"); } } class Musician { private Instrument[] instruments; public Musician(Instrument[] instruments) { this.instruments = instruments; } public void perform() { System.out.println("Musician is ready to perform:"); for (Instrument instrument : instruments) { instrument.play(); } } } public class Composition { public static void main(String[] args) { // Create instances of different instruments Instrument tabla = new Instrument("Tabla"); Instrument sitar = new Instrument("Sitar"); Instrument flute = new Instrument("Flute"); Instrument veena = new Instrument("Veena"); Instrument harmonium = new Instrument("Harmonium"); // Create an array of instruments Instrument[] instruments = { tabla, sitar, flute, veena, harmonium }; // Create a musician with the array of instruments Musician musician = new Musician(instruments); // Musician performs with all instruments musician.perform(); } }
Output:
D:\>java CompositionDemo
Musician is ready to perform:
Tabla is played
Sitar is played
Flute is played
Veena is played
Harmonium is played
Inheritance
Inheritance establishes an “is-a” relationship between classes, where a subclass inherits properties and behaviors from its superclass. Subclasses can extend the functionality of the superclass, promoting code reuse and hierarchical organization. In Java, the extends keyword is used to create a subclass.
class Vehicle {
/* Vehicle properties and methods */
}
class Car extends Vehicle {
/* Car-specific properties and methods */
}
class Truck extends Vehicle {
/* Truck-specific properties and methods */
}
Code language: Java (java)
Delegation
Delegation involves passing on responsibilities or method calls to another object. Instead of inheriting behaviors, a class delegates tasks to another class that specializes in handling them. This allows for flexible, loosely coupled designs and is implemented by creating an instance of another class within the class that requires its functionality.
interface Printer {
void print(String text);
}
class LaserPrinter implements Printer {
public void print(String text) {
// Logic to print using a laser printer
}
}
class PrintManager {
private Printer printer;
public PrintManager(Printer printer) {
this.printer = printer;
}
public void printDocument(String document) {
printer.print(document);
}
}
Code language: Java (java)
Here’s an example that demonstrates deligation:
class Singer { public void sing() { System.out.println("Singer sings a song"); } } class Musician { private Singer singer; public Musician(Singer singer) { this.singer = singer; } public void playInstrument() { System.out.println("Musician plays an instrument"); } public void perform() { playInstrument(); singer.sing(); // Delegating the singing task to the Singer class } } public class Deligation { public static void main(String[] args) { // Create instances of Singer and Musician Singer singer = new Singer(); Musician musician = new Musician(singer); // Musician performs, which involves playing an instrument and singing musician.perform(); } }
Output:
D:\>java DeligationDemo
Musician plays an instrument
Singer sings a song