IsA and HasA Relationship in C# with Examples
ูู ุงูู
ูุงู ุฏูุ ููุชููู
ุนู ุนูุงูุงุช ุงูู IsA ูุงูู HasA ูู C# ุจุงูุฃู
ุซูุฉ. ูุง ุฑูุช ุชูุฑุง ู
ูุงููุง ุนู ุงูู Inheritance ูู C# ูุจู ู
ุง ุชูู
ู ููุง.
ู
ูู
ุฌุฏูุง ูููู
ุงููุฑู ุจูู ุนูุงูุฉ ุงูู IS-A ูุงูู HAS-A ุจูู ุงูู classes. ูุชูุฑ ู
ู ุงูุทูุจุฉ ูุงูู
ุทูุฑููุ ูุฎุตูุตูุง ุงูู
ุจุชุฏุฆููุ ุจูุชูุฎุจุทูุง ุจูู ุงูุนูุงูุชูู ุฏูู. ูุจุณุจุจ ูุฏูุ ุจูุนู
ููุง ุงูุนูุงูุงุช ุจูู ุงูู classes ุจุชุฑุชูุจ ุบูุท.
ุนุดุงู ูุฏูุ ุงูููุงุฑุฏุฉ ูุดุฑุญ ุฅูู ูู ุจุงูุธุจุท ุนูุงูุงุช ุงูู IS-A ูุงูู HAS-Aุ ูุฅูู ุงููุฑูู ุจูููู
ุ ูุฅู
ุชู ูุฅุฒุงู ูุณุชุฎุฏู
ูู
ูู ุงูุชุทุจููุงุช ุงููุงูุนูุฉ. ููุงุ ููุฑูู ุฃู
ุซูุฉ ุจูุบุฉ C#ุ ูุงูู
ูููู
ุฏู ุจููุทุจู ุนูู ุฃู ูุบุฉ ุจุฑู
ุฌุฉ ุดูุฆูุฉ ุชุงููุฉ ุฒู Java, C++ุ ุฅูุฎ.
IsA and HasA Relationship in C#
ูู C#ุ ุฒู ูุบุงุช ุจุฑู
ุฌุฉ ุดูุฆูุฉ ูุชูุฑุ ู
ูุงููู
ุนูุงูุงุช ุงูู โIsAโ ูุงูู โHasAโ ุฃุณุงุณูุฉ ุนุดุงู ูููู
ุงูู inheritance ูุงูู composition ุจุงูุชุฑุชูุจ. ุงูุนูุงูุงุช ุฏู ุจุชุณุงุนุฏ ูู ุชุตู
ูู
ูููู ุงูู classes ูุงูู objects ูู ุงูุจุฑูุงู
ุฌ.
IsA Relationship (Inheritance)
- Definition: ุนูุงูุฉ ุงูู
IsAุจุชุชุญูู ุนู ุทุฑูู ุงููinheritance. ูู ุจุชู ุซู ุนูุงูุฉ ูุฑู ูุฉ (hierarchical) ุจููbase class(parent) ูderived classes(children). ูู ุนูุงูุฉ ุงููIsAุ ุงููderived classุจูููู ูุณุฎุฉ ู ุชุฎุตุตุฉ (specialized version) ู ู ุงููbase class. - Characteristics:
- ุงูู Inheritance: ุงูู
derived classุจููุฑุซ ุงูุฎุตุงุฆุต (properties) ูุงููmethodsู ู ุงููbase class. - ุงูู Polymorphism: ุงูู
derived classููุฏุฑ ูุนู ูoverrideุฃูextendููุธุงุฆู ุงููbase class. - ุงูู Substitutability: ุงูู
Objectsุจุชุงุนุฉ ุงููderived classู ู ูู ูุชุนุงู ู ู ุนุงูุง ูุฃููุงobjectsู ู ุงููbase class.
- ุงูู Inheritance: ุงูู
HasA Relationship (Composition)
- Definition: ุนูุงูุฉ ุงูู
HasAุจุชูุณุชุฎุฏู ุนุดุงู ุชุฏู ุนูู ุงูุงุณุชุฎุฏุงู ุฃู ุงูุชูููู (composition). ูู ุจุชูุถุญ ุฅูobjectู ูclassู ุนูู โุจูุญุชูู ุนููโ ุฃู โุจูุชููู ู ูโobjectsู ูclassุชุงูู. ุงูุนูุงูุฉ ุฏู ุฃูู ุชุฑุงุจุทูุง (tightly coupled) ู ู ุงููinheritance. - Characteristics:
- ุงูู Composition: ุงูู
classุจูุญุชูู ุนูู ุฃู ุจูุชููู ู ูobjectsู ูclassุชุงูู. - ุงูู Independence: ุงูู
objectุงูู ุญุชูู (ุฒูEngine) ููุฏุฑ ูููู ู ูุฌูุฏ ุจุดูู ู ุณุชูู ุนู ุงูุญุงููุฉ (ุฒูCar). - ุงูู Encapsulation: ุทุฑููุฉ ุงูุนู
ู ุงูุฏุงุฎููุฉ ููู
objectุงูู ุญุชูู ุจุชููู ุนุงุฏุฉู ู ุฎููุฉ ุนู ุงููclassุงูุฎุงุฑุฌู. - Flexibility and Reusability: ุงูู
Objectsู ู ูู ุงุณุชุจุฏุงููุง ุฃู ุชุบููุฑูุง ุจุณูููุฉุ ูุฏู ุจูุฏู ู ุฑููุฉ ุฃูุจุฑ ูู ุชุตู ูู ุงูููุฏ.
- ุงูู Composition: ุงูู
IsA and HasA Relationship in C# with Examples
ุฎูููุง ูููู
ุนูุงูุฉ ุงูู IS-A ูุงูู HAS-A ูู C# ุจู
ุซุงู. ุจุต ุนูู ุงูู
ุซุงู ุงููู ุชุญุช:
class Rectangle{
// Some Data Members
// Some Member Functions
}ุฏู class ุงุณู
ู Rectangle. ุฎูููุง ููุชุฑุถ ุฅู ููู ุดููุฉ data members ูmember functions ุฌูู ุงูู class ุฏู. ุจุนุฏ ูุฏูุ ุนูุฏูุง class ุงุณู
ู Cuboidุ ุจููุฑุซ ู
ู Rectangle class ุงููู ููู ูุงูุชุงูู:
class Cuboid : Rectangle{
// Some Data Members
// Some Member Functions
}ุฏู Cuboid classุ ุงููู ุจููุฑุซ ู
ู Rectangle class. ูุจุฑุถู ุงูุชุฑุถ ุฅู ุงูู class ุฏู ููู ุดููุฉ data members ู member functions ุฌูุงู. ุฏูููุชู ุฎูููุง ููุชุจ class ูู
ุงู ูุงูุชุงูู:
class Table{
Rectangle top;
int legs;
}ุฏู Table classุ ู
ุด ุจููุฑุซ ู
ู ุฃู class. ุงูู class ุฏู ููู ุงุชููู data members. ูุงุญุฏ ู
ููู
ู
ู ููุน Rectangle classุ ูุงูุชุงูู ู
ุชุบูุฑ ู
ู ููุน integerุ ุงููู ูู
ุง top ูlegs.
ุฅุฐูุงุ ุงูุฃูู ุนู
ููุง class ุงุณู
ู Rectangle. ุจุนุฏูู ุนู
ููุง class ุชุงูู ุงุณู
ู Cuboidุ ุจููุฑุซ ู
ู Rectangle class. ูุจุนุฏูู ุนู
ููุง class ุชุงูุช ุงุณู
ู Table. ุฌูู Table classุ ุนู
ููุง ู
ุชุบูุฑ ู
ู ููุน Rectangle ูู
ุชุบูุฑ ุชุงูู ู
ู ููุน integer.
ุงูู class Cuboid ุจููุฑุซ ู
ู Rectangle class. ููู ููุฏุฑ ูููู ุฅู ุงูู Cuboid IS A Rectangleุ ุฃููู. ูุงูุนูุงูุฉ ุจูู ุงูู Rectangle ูุงูู Cuboid classes ูู ุนูุงูุฉ IS-A. ูุงูููุทุฉ ุงููู ูุงุฒู
ุชูุชูุฑูุง ูู ุฅููุง ูู
ุง ุจูุนู
ู Inheritance ุฃู ุนูุงูุฉ Parent-Childุ ููุฏุฑ ูููู ุฅู ุงูุนูุงูุฉ ุฏู ูู ุนูุงูุฉ IS-A ูู ูุบุงุช ุงูุจุฑู
ุฌุฉ ุงูุดูุฆูุฉ.
ุจุนุฏ ูุฏูุ Table class ุจุชุงุนูุง ููู ู
ุชุบูุฑ top ู
ู ููุน Rectangular. ุฏู ู
ุนูุงู ุฅู Table class ููู object ู
ู Rectangle class. ููู ููุฏุฑ ูููู ุฅู Table class HAS A Rectangleุ ุฃูููุ ุงูู Table class Has a Rectangle. ูุงูุนูุงูุฉ ุจูู ุงูู Table ูุงูู Rectangle class ูู ุนูุงูุฉ Has A.
ุฅุฐูุงุ ููุฏุฑ ูุณุชุฎุฏู
ุงูู class ุจุชุงุนูุง ุจุทุฑููุชูู: Is A ู Has A. ุฏู ุดุงุฆุน ูู ูุบุงุช ุงูุจุฑู
ุฌุฉ ุงูุดูุฆูุฉุ ู
ุด ุจุณ ูู C#ุ ูู ู
ูุฌูุฏ ูู C++, Javaุ ููุบุงุช ุจุฑู
ุฌุฉ ุดูุฆูุฉ ุชุงููุฉ.
ูุงูู class ูู C# ู
ู
ูู ููุณุชุฎุฏู
ุจุทุฑููุชูู. ุทุฑููุฉ ุจุงุณุชุฎุฏุงู
ุนูุงูุฉ ุงูู inheritanceุ ูุนูู class ููุฑุซ ู
ู class ุชุงูู. ูุงูุทุฑููุฉ ุงูุชุงููุฉ ูู ุฅููุง ูุนู
ู object ู
ู ุงูู class ุฏู ููุณุชุฎุฏู
ู.
Real-time Example to Understand Is-A and Has-A Relationship
ุฎูููุง ูููู
ุนูุงูุงุช ุงูู IS-A ูุงูู HAS-A ุจู
ุซุงู ู
ู ุงููุงูุน. ุฎูููุง ูุงุฎุฏ ู
ุซุงู. ุจุต ุนูู ุงูุตูุฑุฉ ุงููู ุชุญุช:
(ุตูุฑุฉ ุชุตู
ูู
ุนุฑุจูุฉ)
ุฎูููุง ููุชุฑุถ ุฅู ุงูุตูุฑุฉ ุงููู ููู ุฏู ุจุชูุถุญ ุชุตู ูู ุนุฑุจูุชู. ุฏูููุชู ุฃูุง ุฃูุฏุฑ ุฃุตูุน ุฃู ุนุฏุฏ ู ู ุงูุนุฑุจูุงุช ู ู ุงูุชุตู ูู ุงููู ููู ุฏู ูุฃุจูุนูู ูู ุงูุณูู. (ุตูุฑุฉ ุนุฑุจูุงุช ู ุตูุนุฉ)
ุชุตููุน ุงูุนุฑุจูุงุช ู
ุนูุงู ุฅู ุฏู objects. ุฏูููุชูุ ุฃูุง ุฃูุฏุฑ ุฃุจูุน ุชุตู
ูู
ู (ู
ุด ุงูุนุฑุจูุงุช ุงููู ุงุชุตูุนุช ุฃู ุงูู objectsุ ุงูุชุตู
ูู
ุจุณ) ูุดุฑูุชูุ ูุฃูุช ุชูุฏุฑ ุชุตูุน ุนุฑุจูุชู ุจุดููุฉ ุชุบููุฑุงุช ูู ุงูุชุตู
ูู
ุงููู ูุจูุนูููู.
(ุตูุฑุฉ ุชุตู
ูู
ุนุฑุจูุฉ ุฌุฏูุฏุฉ)
So, what are the uses of design class?
ุฅูู ูู ุงุณุชุฎุฏุงู
ุงุช ุงูู design classุ ูุนููุ ุจูุงู
ุทุฑููุฉ ููุฏุฑ ูุณุชุฎุฏู
ุงูู design classุ ููุฏุฑ ูุณุชุฎุฏู
ู ุจุทุฑููุชูู. ูุง ุฅู
ุง ูุดุงุฑูู ู
ุน ุงูู derived classes ุฃู ููุดุฆ object.
- ูู
classุจููุฑุซ ู ูclassุชุงููุ ูุจูู ุนูุฏู ุนูุงูุฉ โIS Aโ ู ุนุงู. - ูู
classุนูุฏูobjectู ูclassุชุงููุ ูุจูู ุนูุฏู ุนูุงูุฉ โHAS Aโ ู ุนุงู.
ุฅุฐูุงุ Table has a Rectangleุ ูCuboid is a Rectangle.
ุจุนุฏ ูุฏูุ ุงูุญุงุฌุฉ ุงูู
ูู
ุฉ ูู ุฅู ุงูู class ู
ู
ูู ูููู ููู ุฃููุงุน ู
ุฎุชููุฉ ู
ู ุงูู members. ุฎูููุง ููุชุฑุถ ุฅู ุนูุฏูุง ุชูุงุช ุฃููุงุน: private, protected, public.
ุฅูู ุงููู ุจูููู ู
ุชุงุญ ุฌูู ุงูู classุ ูุฅูู ุงููู ู
ุชุงุญ ูู ุงูู class ุงููู ููู ุนูุงูุฉ โIS Aโุ ูุฅูู ุงููู ู
ุชุงุญ ูู ุงูู class ุงููู ููู ุนูุงูุฉ โHAS Aโุ
- ูู ุงูู
membersู ุชุงุญุฉ ุฌูู ุงููclassููุณู. - ุงูู
publicูุงููprotected membersู ุชุงุญูู ูู ุงููderived classes(ุนูุงูุฉ IS-A). - ุงูู
public membersุจุณ ูู ุงููู ู ุชุงุญุฉ ู ู ุงููnon-derived classes(ุนูุงูุฉ HAS-A).
Example to Understand IS-A Relationship in C#
ุจุต ุนูู ุงูู
ุซุงู ุงููู ุชุญุช. ููุงุ ุงูุนูุงูุฉ ุจูู Rectangle ู Cuboid ูู ุนูุงูุฉ IS-Aุ ูููุฏุฑ ูููู ุฅู Cuboid IS-A Rectangle.
using System;
namespace IsAHasADemo
{
public class Program
{
static void Main()
{
Cuboid cuboid = new Cuboid(3, 5, 7);
Console.WriteLine($"Volume is : {cuboid.Volume()}");
Console.WriteLine($"Area is : {cuboid.Area()}");
Console.WriteLine($"Perimeter is : {cuboid.Perimeter()}");
Console.ReadKey();
}
}
class Rectangle
{
// Data Members
public int Length;
public int Breadth;
// Member Functions
public int Area()
{
return Length * Breadth;
}
public int Perimeter()
{
return 2 * (Length + Breadth);
}
}
// Establishing Parent-Child Relationship
// IS-A Relationship i.e. Cuboid IS-A Rectangle
class Cuboid : Rectangle
{
public int Height;
public Cuboid(int l, int b, int h)
{
Length = l;
Breadth = b;
Height = h;
}
public int Volume()
{
return Length * Breadth * Height;
}
}
}Output:
Volume is : 105
Area is : 15
Perimeter is : 16
Example to Understand HAS-A Relationship in C#
ุจุต ุนูู ุงูู
ุซุงู ุงููู ุชุญุช. ููุงุ ุฌูู Employee classุ ุนู
ููุง ู
ุชุบูุฑ ู
ู ููุน Addressุ ูุฏู ู
ุด ุฃูุชุฑ ู
ู ุนูุงูุฉ HAS-A ุจูู Employee ูAddress. ูููุฏุฑ ูููู ุฅู Employee HAS-A Address.
using System;
namespace IsAHasADemo
{
public class Program
{
static void Main()
{
Address address = new Address("B1-3029", "BBSR", "Odisha");
Employee employee = new Employee(1001, "Ramesh", address);
employee.Display();
Console.ReadKey();
}
}
class Address
{
public string AddressLine, City, State;
public Address(string addressLine, string city, string state)
{
AddressLine = addressLine;
City = city;
State = state;
}
}
class Employee
{
// Using Address in Employee class
// Establishing Has-A relationship i.e. Employee HAS-A Address
public Address address;
public int Id;
public string Name;
public Employee(int id, string name, Address adrs)
{
Id = id;
Name = name;
address = adrs;
}
public void Display()
{
Console.WriteLine($"Employee Id: {Id}");
Console.WriteLine($"Employee Name: {Name}");
Console.WriteLine($"AddressLine: {address.AddressLine}");
Console.WriteLine($"City: {address.City}");
Console.WriteLine($"State: {address.State}");
}
}
}Output:
Employee Id: 1001
Employee Name: Ramesh
AddressLine: B1-3029
City: BBSR
State: Odisha
ูู ุงูู
ุซุงู ุงููู ูููุ ูู ุฎูููุง Employee Class ููุฑุซ ู
ู Address classุ ูุฏู ู
ุด ููููู ููู ุฃู ู
ุนูู. ูู ูุฑูุซูุงูุ ูููุดุฆ ุนูุงูุฉ IS-A ุจูู Employee ูAddress classesุ ูู
ููุด ู
ุนูู ุฅููุง ูููู Employee IS-A Address. ุจุงูุนูุณุ ูู ุทุจููุง ุนูุงูุฉ HAS-Aุ ูุจูุจูู ูููุง ู
ุนูู ุฅููุง ูููู Employee HAS-A Address.
Differences Between IsA and HasA Relationship in C#
- Nature of Relationship:
IsAูู ุนูุงูุฉinheritance(parent-child)ุ ุจููู ุงHasAูู ุนูุงูุฉcompositional(ุชูููููุฉ). - Coupling:
IsAุนุงุฏุฉู ุจุชุนูู ุชุฑุงุจุท ุฃููู (stronger coupling) ุจูู ุงููclassesุ ูุฅู ุงููderived classุจูููู ู ุฑุชุจุท ุจุดูู ูุซูู ุจุงููbase classุจุชุงุนู.HasAุจุชุนูู ุชุฑุงุจุท ุฃุถุนู (looser coupling)ุ ูุฅู ุงููobjectsู ู ูู ุชููู ู ูุฌูุฏุฉ ุจุดูู ู ุณุชูู. - Usage:
IsAุจุชูุณุชุฎุฏู ูู ุงclassูุญุชุงุฌ ูููู ุงู ุชุฏุงุฏ ููclassุชุงููุ ูููุฑุซ ุฎุตุงุฆุตู ูุณููููุงุชู.HasAุจุชูุณุชุฎุฏู ูู ุงclassูุญุชุงุฌ ูุธุงุฆู ุฃู ุฎุตุงุฆุต ู ุนููุฉ ู ูุฌูุฏุฉ ูู ุชุบููุฉ (encapsulated) ููclassุชุงูู. - Flexibility: ุงูู
Composition(HasA) ุจูุฏู ู ุฑููุฉ ุฃูุจุฑ ูุฅู ุงูู ูููุงุช (components) ู ู ูู ุงุณุชุจุฏุงููุง ุฃู ุชุนุฏูููุง ุจุณูููุฉ ู ู ุบูุฑ ู ุง ุชุฃุซุฑ ุนูู ุงููcontainer class.
How do you decide what to implement between IS-A and HAS-A?
ุงุณุฃู ููุณู ุงูุณุคุงู. ู ุซููุงุ ูู ุณุฃูุชูุ ุฃููู ุฌู ูุฉ ูููุง ู ุนูู ุฃูุชุฑ ู ู ุงูุฌู ูุชูู ุงููู ุชุญุช ุฏููุ
Employee IS-A AddressEmployee HAS-A Address
ุฃููุฏ ูุชููู ุฅู Employee HAS-A Address ูููุง ู
ุนูู ุฃูุชุฑ ู
ู Employee IS-A Address.
ุทูุจ ูู ุณุฃูุชูุ ุฃููู ุฌู ูุฉ ูููุง ู ุนูู ุฃูุถู ู ู ุงูุงุชููู ุฏููุ
BMW IS-A CarBMW HAS-A Car
ุฅุฌุงุจุชู ุฃููุฏ ูุชููู BMW IS-A Car.
ู
ูุงุญุธุฉ: ูู C#ุ ุนูุงูุฉ ุงูู IS-A ุจุชุชุนู
ู ุจุงุณุชุฎุฏุงู
ุงูู Inheritanceุ ูุนูุงูุฉ ุงูู HAS-A ุจุชุชุนู
ู ุจุงุณุชุฎุฏุงู
ุงูู Compositionุ ูุนูู ุฅูู ุชุนูู ุนู ู
ุชุบูุฑ. ููู ู
ุง ุจูุนูู ุนู ู
ุชุบูุฑ ู
ู class ุฌูู class ุชุงููุ ุจูุณู
ู ุฏู Compositionุ ุฃู ุชูุฏุฑ ุชููู ุนูุงูุฉ HAS-A.