Header Ads

[OOP] Class relationships


Nguồn: Click here

1. Association

Let's say we have two classes: "Student" and "Teacher".

A student can be associated with multiple teachers and a teacher can have multiple students.
The Student class has a reference to the Teacher class and the Teacher class has a reference to the Student class.
Here's an example in Java code:

class Student {
  private String name;
  private Teacher[] teachers;
 
  // constructors, getters, setters
}

class Teacher {
  private String name;
  private Student[] students;
 
  // constructors, getters, setters
}

In this example, each Student object has a reference to multiple Teacher objects and each Teacher object has a reference to multiple Student objects. This represents the Association relationship between the two classes.

2. Aggregation

Let's say we have two classes: "Department" and "Employee".

A department can have multiple employees and an employee can work for multiple departments.
The Department class has a reference to the Employee class, but the lifecycle of the Employee class is not controlled by the Department class.
Here's an example in Java code:

class Department {
  private String name;
  private List<Employee> employees;
 
  // constructors, getters, setters
}

class Employee {
  private String name;
  private int employeeId;
 
  // constructors, getters, setters
}

In this example, each Department object has a reference to multiple Employee objects, but the lifecycle of Employee objects is not controlled by the Department class. This represents the Aggregation relationship between the two classes.

3. Composition

Let's say we have two classes: "Car" and "Engine".

A car must have an engine and an engine cannot exist without a car.
The Car class has a reference to the Engine class, and the lifecycle of the Engine class is controlled by the Car class.
Here's an example in Java code:

class Car {
  private Engine engine;
 
  // constructors, getters, setters
}

class Engine {
  private int engineCapacity;
 
  // constructors, getters, setters
}
In this example, each Car object has an Engine object, and the lifecycle of the Engine object is controlled by the Car object. This represents the Composition relationship between the two classes.

4. Inheritance

 
 Let's say we have two classes: "Animal" and "Dog".

The Dog class is a more specialized type of Animal.
The Dog class inherits properties and behavior from the Animal class.
Here's an example in Java code:

csharp
Copy code
class Animal {
  private String name;
  private int age;
 
  public void makeSound() {
    System.out.println("Animal sound");
  }
 
  // constructors, getters, setters
}

class Dog extends Animal {
  private String breed;
 
  @Override
  public void makeSound() {
    System.out.println("Bark");
  }
 
  // constructors, getters, setters
}
In this example, the Dog class extends the Animal class and inherits properties and behavior from the Animal class. The Dog class can also override the makeSound method to provide its own implementation. This represents the Inheritance relationship between the two classes.
 

5. Interface implementation

 
The Generalization - Implementation relationship, also known as implementation, is a type of relationship in object-oriented programming (OOP) where a class implements an interface. An interface is a collection of abstract methods that defines a contract for a class to implement.

In OOP, an interface defines a set of common methods that must be implemented by any class that implements the interface. By implementing an interface, a class commits to implementing all the methods defined in the interface. This enables you to create a hierarchy of interfaces and promote code reuse and encapsulation.

Here's a simple example in Java:
public interface Shape {
  double getArea();
}

public class Square implements Shape {
  private double side;
 
  public Square(double side) {
    this.side = side;
  }
 
  public double getArea() {
    return side * side;
  }
}
In this example, the Shape interface defines an abstract method getArea() that must be implemented by any class that implements the interface. The Square class implements the Shape interface and provides an implementation for the getArea() method. The Square class implements the contract defined by the Shape interface and can be used wherever a Shape object is required.

Không có nhận xét nào

Được tạo bởi Blogger.