๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Relationship Between Classes in Java  


Introduction
  • Relationship between classes describes that how multiple classes interact with or depend on each other.
  • These relationships help structure and organize code in a logical and maintainable way.
  • Types of relationships between classes in Java:
    1. Association (HAS-A relationship)
    2. Dependency (USES-A relationship)
    3. Inheritance (IS-A relationship)

1. Association (HAS-A relationship)
  • Definition : Association is a relationship where one class interacts with another class.
  • Example :
    • Student HAS-A Address
    • Car HAS-A Engine.
  • How to achieve association ?
    • Association is achieved by declaring object references as instance variables inside a class.
  • Program :
    class Address
    {
        String city = "Delhi";
        String country = "India";
    
        void displayAddress()
        {
            System.out.println("City: " + city + ", Country: " + country);
        }
    }
    
    class Student
    {
        String name = "Deepak";
        int rollno = 101;
    
        // Direct reference to another class
        Address address = new Address();  // Object created directly inside the class
    
        void displayInfo()
        {
            System.out.println("Name: " + name + ", Roll No: " + rollno);
            address.displayAddress();
        }
    }
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            Student student = new Student();  // No need to pass Address
            student.displayInfo();            // Displays student info along with address
        }
    }
    Output:
    Name: Deepak, Roll No: 101
    City: Delhi, Country: India

2. Dependency (USES-A relationship)
  • Definition :
    • Dependency is a relationship where one class uses another class to perform a specific task.
    • It typically exists when one class depends on another temporarily, usually within a method.
  • Example : Office Worker USES-A Printer.
  • How to achieve dependency ?
    • Dependency can be achieved through:
      • Local variables inside methods
      • Method parameters
  • Program :
    class Printer
    {
        void printDocument(String doc)
        {
            System.out.println("Printing document: " + doc);
        }
    }
    
    class OfficeWorker
    {
        void doWork()
        {
            Printer printer = new Printer(); // Dependency via local variable
            printer.printDocument("ProjectReport.pdf");
            System.out.println("Work completed.");
        }
    }
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            OfficeWorker worker = new OfficeWorker();
            worker.doWork(); // OfficeWorker depends on Printer to print
        }
    }
    Output:
    Printing document: ProjectReport.pdf
    Work completed.

3. Inheritance (IS-A relationship)
  • Definition : IS-A (Inheritance) is the process by which a child class (subclass) inherits fields and methods from a parent class (superclass).
  • Example : A Car IS-A Vehicle.
  • How to achieve inheritance ?
    • Inheritance is achieved using:
      • extends keyword in case of classes.
      • implements keyword in case of interfaces.
  • Program :
    class Vehicle
    {
        void start()
        {
            System.out.println("Vehicle starts.");
        }
    }
    
    class Car extends Vehicle
    {
        void drive()
        {
            System.out.println("Car drives.");
        }
    }
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            Car myCar = new Car();
    
            myCar.start(); // inherited from Vehicle
            myCar.drive(); // specific to Car
        }
    }
    Output:
    Vehicle starts.
    Car drives.