arttuladhar
7/16/2018 - 9:45 PM

Aggregation Demo

Aggregation Demo

package com.art.fundamentals;

public class AggregationDemo {
  public static void main(String[] args) {
    Engine dohc_v6 = new Engine("3.0L TFSI Supercharged DOHC V-6", "2014", "Audi");
    Car myCar = new Car(dohc_v6, "Audi A6");

    // Car Info
    System.out.println(myCar.toString());
    myCar = null;

    // Engine Info
    System.out.println(dohc_v6.toString());
  }
}


class Car {
  Engine engine;
  String model;

  public Car(Engine engine, String model) {
    this.engine = engine;
    this.model = model;
  }

  @Override
  public String toString() {
    return "Car{" +
        "engine=" + engine +
        ", model='" + model + '\'' +
        '}';
  }
}

class Engine {
  String name;
  String year;
  String brand;

  public Engine(String name, String year, String brand) {
    this.name = name;
    this.year = year;
    this.brand = brand;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getYear() {
    return year;
  }

  public void setYear(String year) {
    this.year = year;
  }

  public String getBrand() {
    return brand;
  }

  public void setBrand(String brand) {
    this.brand = brand;
  }

  @Override
  public String toString() {
    return "Engine{" +
        "name='" + name + '\'' +
        ", year='" + year + '\'' +
        ", brand='" + brand + '\'' +
        '}';
  }
}