Sunday, August 12, 2018

What is Is-A relationship and Has-A relationship.


Inheritance is a example of IS-A relationship and Composition,Association and aggregation  relationship is example of
HAS-A relationship.

Generally, these four types of relationships (Inheritance, Composition, Association and Aggregation) are used in OOPS.


IS-A relationship:

It is totally based on the inheritance.

For better understanding let us take a real world scenario.
  • HOD is a staff member of college.
  • All teachers are staff member of college.
  • HOD and teachers has id card to enter into college.
  • HOD has a staff that work according the instruction of him.
  • HOD has responsibility to undertake the works of teacher to cover the course in fixed time period.

Example:

  1. class StaffMember  
  2.     {  
  3.         public StaffMember()  
  4.         {  
  5.   
  6.         }  
  7.     }  
  8.   
  9.     class HOD : StaffMember  
  10.     {  
  11.         public HOD()  
  12.         {  
  13.   
  14.         }  
  15.     }  
  16.   
  17.     class Teacher : StaffMember  
  18.     {  
  19.         public Teacher()  
  20.         {  
  21.   
  22.         }  
  23.     } 



Composition:

Composition is a "part-of" relationship. Simply composition means mean use of instance variables that are references to other objects. In composition relationship both entities are interdependent of each other for example “engine is part of car”, “heart is part of body"


Example:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using static System.Console;  
  6.   
  7. namespace Entity2  
  8. {  
  9.    class Car{  
  10.         public Car() { }  
  11.         public string Color { getset; }  
  12.         public string Max_Speed { getset; }  
  13.     }  
  14.       
  15.   
  16.     class Suzuki:Car  
  17.     {  
  18.         public Suzuki() { }  
  19.         public int Total_Seats { getset; }  
  20.         public string Model_No { getset; }  
  21.         public void CarInfo()  
  22.         {  
  23.             string Info=$"Color Of car is {this.Color} \n Maximum Speed is {this.Max_Speed}\n Numbers of Seets is\n  {this.Total_Seats} Model_No is {this.Model_No}  \n";  
  24.             WriteLine(Info);  
  25.             Engine Obj = new Engine();  
  26.             Obj.Engine_Info();  
  27.         }  
  28.     }  
  29.      
  30.     class Engine  
  31.     {  
  32.         public void Engine_Info()  
  33.         {  
  34.             WriteLine("Engine is 4 stroke and fuel efficiency is good");  
  35.         }  
  36.     }  
  37.     class Program  
  38.     {  
  39.         static void Main(string[] args)  
  40.         {  
  41.             Suzuki Obj = new Suzuki();  
  42.             Obj.Color = "Black";  
  43.             Obj.Max_Speed = "240KM/Hour";  
  44.             Obj.Model_No = "SUZ234";  
  45.             Obj.Total_Seats = 4;  
  46.             Obj.CarInfo();  
  47.             ReadLine();  
  48.         }  
  49.     }  
  50.   
  51. }  


Association:


Association is a “has-a” type relationship. Association establish the relationship b/w two classes using through their objects. Association relationship can be one to one, One to many, many to one and many to many.

Example:

  1. class Employee  
  2.     {  
  3.         public Employee() { }  
  4.         public string Emp_Name { getset; }  
  5.   
  6.         public void Manager_Name(Manager Obj)  
  7.         {  
  8.             Obj.manager_Info(this);  
  9.         }  
  10.     }  
  11.   
  12.     class Manager  
  13.     {  
  14.          
  15.         public Manager() { }  
  16.         public string Manager_Name { getset; }  
  17.         public void manager_Info(Employee Obj)  
  18.         {  
  19.             WriteLine($"Manager of Employee  {Obj.Emp_Name} is  {this.Manager_Name}");  
  20.   
  21.         }  
  22.   
  23.   
  24.   
  25.     }  
  26.     class Program  
  27.     {  
  28.         
  29.   
  30.         static void Main(string[] args)  
  31.         {  
  32.             Manager Man_Obj = new Manager();  
  33.             Man_Obj.Manager_Name = "Dazy Aray";  
  34.             Employee Emp_Obj = new Employee();  
  35.             Emp_Obj.Emp_Name = "Ambika";  
  36.             Emp_Obj.Manager_Name(Man_Obj);             
  37.             ReadLine();  
  38.         }  
  39.     }  



Aggregation:

Aggregation is based is on "has-a" relationship. Aggregation is a special form of association. In association there is not any classes (entity) work as owner but in aggregation one entity work as owner. In aggregation both entities meet for some work and then get separated. Aggregation is a one way association. 

Example

Let us take an example of “Student” and “address”. Each student must have an address so relationship b/w Student class and Address class will be “Has-A” type relationship but vice versa is not true(it is not necessary that each address contain by any student). So Student work as owner entity. This will be a aggregation relationship.

Example:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using static System.Console;  
  6.   
  7. namespace Entity2  
  8. {  
  9.        class Student_  
  10.     {  
  11.         public Student_() { }  
  12.         public string Name { getset; }  
  13.         public int roll_No { getset; }  
  14.         public int Class { getset; }  
  15.         public void Get_Student_Info(Address Obj)  
  16.         {  
  17.             WriteLine($"Student Name={this.Name}\n Roll_No={this.roll_No}\n Class={this.Class}\n");  
  18.             Obj.Get_Address();  
  19.         }  
  20.     }  
  21.       
  22.     class Address  
  23.     {  
  24.         public Address() { }  
  25.         public String Street { getset; }  
  26.         public string City { getset; }  
  27.         public string State { getset; }  
  28.         public string Pincode { getset; }  
  29.         public void Get_Address()  
  30.         {  
  31.             WriteLine($"Street={this.Street} \n City={this.City} \n State={this.State}\n Pincode={this.Pincode}");  
  32.         }  
  33.     }  
  34.     class Program  
  35.     {   
  36.         static void Main(string[] args)  
  37.         {  
  38.             Student_ Stu_Obj=new Student_();  
  39.             Stu_Obj.Name = "Pankaj Choudhary";  
  40.             Stu_Obj.roll_No = 1210038;  
  41.             Stu_Obj.Class = 12;  
  42.             Address Obj = new Address();  
  43.             Obj.City = "Alwar";  
  44.             Obj.Street = "P-20 Gnadhi Nagar";  
  45.             Obj.State = "Rajasthan";  
  46.             Obj.Pincode = "301001";  
  47.             Stu_Obj.Get_Student_Info(Obj);  
  48.             ReadLine();  
  49.         }  
  50.     }  
  51.   
  52. }  






No comments:

Post a Comment