Thursday, June 17, 2021

Java OOPs: Relationship Concepts(IS-A and HAS-A)

Chapters

Java "IS-A" Relationship

"IS-A" relationship is also known as Inheritance. It can be divided into two types: class inheritance also known as generalization and interface inheritance also known as realization.

Java "HAS-A" Relationship

"HAS-A" relationship refers to objects that are in possession of classes. It is also known as association.

Association

Association integrates relationship of classes by using an object reference. Association relationships can be one to one, one to many, many to one, and many to many.

Aggregation

Aggregation is a type of association where objects that are in possession of a class are independent from each other.
public class SampleClass{
  
  //The association here is many to one. many
  //object references in one class
  public static void main(String[]args){
    Hammer hammer = new Hammer("hammer");
    Drill drill = new Drill("drill");
    //create toolbox with items
    Toolbox toolBox = new Toolbox(hammer,drill);
    toolBox.checkItems();
    
    //create toolbox without items
    toolBox = new Toolbox();
    toolBox.checkItems();
    
    hammer.smash();
    drill.drill();
  }
}

abstract class Tools{
  private String name;
  
  Tools(String name){
    this.name = name;
  }
  
  public String getName(){return name;}
}

class Hammer extends Tools{

  Hammer(String name){
    super(name);
  }
  
  public void smash(){
    System.out.println("Hammer Smash!");
  }
}

class Drill extends Tools{

  Drill(String name){
    super(name);
  }
  
  public void drill(){
    System.out.println("Drill!");
  }
}

class Toolbox{
  private Tools[] tools;
  
  Toolbox(Tools... tools){
    this.tools = new Tools[tools.length];
    
    for(int i = 0; i < tools.length; i++)
      this.tools[i] = tools[i];
  }
  
  public void checkItems(){
    if(tools.length != 0){
      System.out.println("Items in the Toolbox");
      for(Tools tool : tools)
        System.out.println(tool.getName());
    }
    else
      System.out.println("There are no tools in the Toolbox!");
    
  }
  
}
In the example above, SampleClass class possesses Toolbox and Tools object references. the Toolbox object can still function as a toolbox even without tools. On the other hand, Tools objects can still function as tools without toolbox. We can check items in the Toolbox object even there are no Tools object in it and we can use tools even without toolbox. That's why Toolbox object is independent from Tools object and vice-versa.

Composition

Composition is a type of association where objects that are in possession of a class are dependent from each other.
public class SampleClass{

  public static void main(String[]args){
    
    /*
    RandomAccessMemory ram = null;
    Motherboard board = new Motherboard();
    
    //error: can't use Motherboard 'cause there's
    //no RAM
    board.useMotherboard(ram);
    */
    
    RandomAccessMemory ram = new RandomAccessMemory();
    Motherboard board = new Motherboard();
    ram.useRAM(board);
    System.out.println();
    board.useMotherboard(ram);
  }
}

class RandomAccessMemory{
  
  void useRAM(Motherboard board){
    if(board == null)
      throw new NullPointerException("Can't use RAM because "
                +"there's no motherboard.");
    
    System.out.println("Motherboard found!");
    System.out.println("Plugging RAM into the motherboard...");
    System.out.println("RAM is plugged in!");
    System.out.println("Motherboard is available for use!");
  }
}

class Motherboard{

  void useMotherboard(RandomAccessMemory ram){
    if(ram == null)
      throw new NullPointerException("Can't use motherboard because "
                +"there's no RAM."); 
    
    System.out.println("RAM found!");
    System.out.println("Plugging RAM into the motherboard...");
    System.out.println("RAM is plugged in!");
    System.out.println("Motherboard is available for use!");
  }
}
In the example above, SampleClass possesses motherboard and RAM object references. RAM is dependent from motherboard in order to function and vice-versa. A motherboard can't function without RAM and vice versa.

No comments:

Post a Comment