Association Relationship in Java

Introduction to Association Relationship between Classes in Java

An association relationship between classes in Java is a relationship where two classes are aware of each other and can interact with each other. This type of relationship is often used to model real-world relationships, such as the relationship between a customer and an order, or the relationship between a student and a course.

Association relationships can be one-to-one, one-to-many, many-to-one, or many-to-many.

  • One-to-one association relationship: A one-to-one association relationship means that one object in one class can only be associated with one object in the other class. For example, a customer can only have one order at a time.
  • One-to-many association relationship: A one-to-many association relationship means that one object in one class can be associated with multiple objects in the other class. For example, a customer can have many orders.
  • Many-to-one association relationship: A many-to-one association relationship means that multiple objects in one class can be associated with one object in the other class. For example, many students can be enrolled in one course.
  • Many-to-many association relationship: A many-to-many association relationship means that multiple objects in one class can be associated with multiple objects in the other class. For example, many students can take many courses.

Association relationships can be implemented in Java using fields, methods, or both. For example, the following code shows a one-to-one association relationship between a `Customer` class and an `Order` class:


public class Customer {
  private Order order;

  public Order getOrder() {
    return order;
  }

  public void setOrder(Order order) {
    this.order = order;
  }
}

public class Order {
  private Customer customer;

  public Customer getCustomer() {
    return customer;
  }

  public void setCustomer(Customer customer) {
    this.customer = customer;
  }
}

In this example, the `Customer` class has a field called `order` that stores a reference to an `Order` object. The `Order` class has a field called `customer` that stores a reference to a `Customer` object.

Association relationships are a powerful tool for modeling real-world relationships in Java. By using association relationships, you can write more robust and reliable code.

Here are some tips for using association relationships effectively:

  • Choose the right type of association relationship for the relationship you are modeling.
  • Use fields or methods to implement association relationships, depending on your specific needs.
  • Document the association relationships in your code so that it is clear how the classes are related to each other.

Exercises

Demo code for Association Relationship between classes in Java


  package sampleproject;
  import java.util.Scanner;
  
  public class democlass {
  
    public static void main(String[] args) {
      
      student s1 = new student("satish","12BCE","99456");
      professor p = new professor();
      p.viewprofile(s1);
    }
  }
  
  class student
  {
    String name;
    String regno;
    String phone;
    
    public student(String name, String regno, String phone) {
      super();
      this.name = name;
      this.regno = regno;
      this.phone = phone;
    }
  
    public String getInfo() {
            return(name+regno+phone);	
    }
  }
  
  class professor
  {
    public void viewprofile(student s1) 
    {
      System.out.println("Enter student id");
      Scanner input = new Scanner(System.in);
      String regno=input.nextLine();
      if(s1.regno.equals(regno))
      {
        System.out.println(s1.getInfo());
      }
    }
  }