Question: // Java Program to Illustrate Difference between Aggregation and Composition // Class 1 // Engine class which will be used by car // and Car
// Java Program to Illustrate Difference between Aggregation and Composition // Class 1 // Engine class which will be used by car // and Car class will have a field/attribute of Engine type.
class Engine { // Method to starting an engine public void work(){ System.out.println("Engine of car has been started "); } }
// Class 2 // Engine class
class Car { // Composition private Engine engine; // Constructor Car(Engine engine){ this.engine = engine; } public void move(){ engine.work(); System.out.println("Car is moving "); } }
// Class 3 // Main class
public class Main{ public static void main(String[] args) { // Making an engine by creating // an instance of Engine class. Engine engine = new Engine(); // Making a car with engine // passing a engine instance as an argument // while creating instance of Car Car car = new Car(engine); // Making car to move //by calling move() method inside main() car.move(); } }
1. Compile and run the program to get the output. Explain the output.
2. Draw the UML class diagram relationships for the program.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
