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:
- class StaffMember
- {
- public StaffMember()
- {
- }
- }
- class HOD : StaffMember
- {
- public HOD()
- {
- }
- }
- class Teacher : StaffMember
- {
- public Teacher()
- {
- }
- }
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:
Example:
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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using static System.Console;
- namespace Entity2
- {
- class Car{
- public Car() { }
- public string Color { get; set; }
- public string Max_Speed { get; set; }
- }
- class Suzuki:Car
- {
- public Suzuki() { }
- public int Total_Seats { get; set; }
- public string Model_No { get; set; }
- public void CarInfo()
- {
- 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";
- WriteLine(Info);
- Engine Obj = new Engine();
- Obj.Engine_Info();
- }
- }
- class Engine
- {
- public void Engine_Info()
- {
- WriteLine("Engine is 4 stroke and fuel efficiency is good");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Suzuki Obj = new Suzuki();
- Obj.Color = "Black";
- Obj.Max_Speed = "240KM/Hour";
- Obj.Model_No = "SUZ234";
- Obj.Total_Seats = 4;
- Obj.CarInfo();
- ReadLine();
- }
- }
- }
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:
- class Employee
- {
- public Employee() { }
- public string Emp_Name { get; set; }
- public void Manager_Name(Manager Obj)
- {
- Obj.manager_Info(this);
- }
- }
- class Manager
- {
- public Manager() { }
- public string Manager_Name { get; set; }
- public void manager_Info(Employee Obj)
- {
- WriteLine($"Manager of Employee {Obj.Emp_Name} is {this.Manager_Name}");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Manager Man_Obj = new Manager();
- Man_Obj.Manager_Name = "Dazy Aray";
- Employee Emp_Obj = new Employee();
- Emp_Obj.Emp_Name = "Ambika";
- Emp_Obj.Manager_Name(Man_Obj);
- ReadLine();
- }
- }
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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using static System.Console;
- namespace Entity2
- {
- class Student_
- {
- public Student_() { }
- public string Name { get; set; }
- public int roll_No { get; set; }
- public int Class { get; set; }
- public void Get_Student_Info(Address Obj)
- {
- WriteLine($"Student Name={this.Name}\n Roll_No={this.roll_No}\n Class={this.Class}\n");
- Obj.Get_Address();
- }
- }
- class Address
- {
- public Address() { }
- public String Street { get; set; }
- public string City { get; set; }
- public string State { get; set; }
- public string Pincode { get; set; }
- public void Get_Address()
- {
- WriteLine($"Street={this.Street} \n City={this.City} \n State={this.State}\n Pincode={this.Pincode}");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Student_ Stu_Obj=new Student_();
- Stu_Obj.Name = "Pankaj Choudhary";
- Stu_Obj.roll_No = 1210038;
- Stu_Obj.Class = 12;
- Address Obj = new Address();
- Obj.City = "Alwar";
- Obj.Street = "P-20 Gnadhi Nagar";
- Obj.State = "Rajasthan";
- Obj.Pincode = "301001";
- Stu_Obj.Get_Student_Info(Obj);
- ReadLine();
- }
- }
- }
No comments:
Post a Comment