Loading subject…
In object-oriented programming (OOP), relationships between objects are fundamental to designing flexible and maintainable systems.
These relationships define how objects interact, collaborate, and depend on each other to solve complex problems.
Imagine a traffic simulation model:
Let's explore each of these relationships in detail.
Dependency can be:
Here, Report depends on Printer to print
In UML diagrams, dependency is represented by a dashed arrow pointing from the dependent class to the class it depends on.
Dependency is useful for allowing objects to interact without being tightly coupled.
When designing systems, aim to minimise dependencies.
Imagine an address book that needs to read contact data from a local file, database, or network.
Characteristics of aggregation:
Consider a Department class that has a Teacher.
The Teacher can exist independently of the Department.
In UML diagrams, aggregation is represented by a hollow diamond connected to the parent class.
Inheritance is an "is-a" relationship, where a subclass inherits attributes and methods from a superclass.
Characteristics of inheritance:
Student and Professor inherit from Person, sharing common attributes and methods.
In UML diagrams, inheritance is represented by a hollow triangle pointing from the subclass to the superclass.
An approximate step-by-step guide is:
class Printer { public void print(String message) { System.out.println(message); }}
class Report { public void generate() { Printer printer = new Printer(); // used temporarily printer.print("Report generated"); }}class Teacher { private String name; // attribute
Teacher(String name) { // constructor this.name = name; }
public String getName() { // getter return name; }}
class Department { private String deptName; private Teacher teacher; // aggregation: Department uses Teacher
public Department(String deptName, Teacher teacher) { this.deptName = deptName; this.teacher = teacher; }
public void showDetails() { System.out.println("Department: " + deptName); System.out.println("Teacher: " + teacher.getName()); } public static void main(String[] args) { Teacher t1 = new Teacher("Dr. Smith"); // teacher exists independently
Department d1 = new Department("Computer Science", t1);
d1.showDetails(); }}
class Person { // super class protected String name; protected int age;
public Person(String name, int age) { this.name = name; this.age = age; }
public void introduce() { System.out.println("Hi, I am " + name + " and I am " + age + " years old."); }}
class Student extends Person { // first subclass private String major;
public Student(String name, int age, String major) { super(name, age); this.major = major; }
public void study() { System.out.println(name + " is studying " + major + "."); }}
class Professor extends Person { // second subclass private String department;
public Professor(String name, int age, String department) { super(name, age); this.department = department; }
public void teach() { System.out.println(name + " is teaching in the " + department + " department."); }}
public class Main { // class for testing public static void main(String[] args) { Student s = new Student("Alice", 20, "Computer Science"); Professor p = new Professor("Dr. Smith", 45, "Mathematics");
s.introduce(); // output: Hi, I am Alice and I am 20 years old. s.study(); // output: Alice is studying Computer Science.
p.introduce(); // output: Hi, I am Dr. Smith and I am 45 years old. p.teach(); // Dr. Smith is teaching in the Mathematics department. }}