Introduction
ูู ุงูู
ูุงู ุฏูุ ูุชููู
ุนู ุฃููุงุน ุงูู Inheritance ูู C# ุจุงูุฃู
ุซูุฉ. ูุง ุฑูุช ุชูุฑุง ู
ูุงููุง ุงููู ูุงุชุ ูุงููู ุดุฑุญูุง ููู ุงูู
ูุงููู
ุงูุฃุณุงุณูุฉ ููู Inheritance ูู ูุบุฉ C# ุจุงูุฃู
ุซูุฉ.
Types of Inheritance in C#
ุฃููุงุน ุงูู Inheritance ุฏู ุจุชูุถุญ ููุง ุนุฏุฏ ุงูู parent classes ุงููู ุงูู child class ุจูู
ุชูููุงุ ุฃู ุนุฏุฏ ุงูู child classes ุงููู ุงูู parent class ุจูู
ุชูููุง.
ุญุณุจ ูุบุฉ C++ุ ูููู ุฃูุง ุจุชููู
ุนู C++ ูู ูุฅู ุงูู Object-Oriented Programming ุธูุฑ ุฃุตููุง ู
ููุงุ ููู ุฎู
ุณ ุฃููุงุน ู
ุฎุชููุฉ ู
ู ุงูู Inheritances:
Single InheritanceMulti-Level InheritanceHierarchical InheritanceHybrid InheritanceMultiple Inheritance
Single Inheritance
ูู
ุง class ุจููุฑุซ ู
ู base class ูุงุญุฏ ุจุณุ ุงูููุน ุฏู ู
ู ุงููุฑุงุซุฉ ุจูุชุณู
ู single inheritance.
ุฒู ู
ุง ุดุงูู ูู ุงูุตูุฑุฉุ ูู ุนูุฏูุง class ุงุณู
ู A ููู ุงูู Parent classุ ูclass ุชุงูู ุงุณู
ู B ููู ุงูู Child classุ ูclass B ุจููุฑุซ ู
ู class A. ุฏู ู
ุนูุงู ุฅู Class B ุนูุฏู parent class ูุงุญุฏ ุจุณ ุงููู ูู class A. ุงูููุน ุฏู ู
ู ุงููุฑุงุซุฉ ุจูุชุณู
ู Single Inheritance.
Multilevel Inheritance
ูู
ุง derived class ุจูุชู
ุฅูุดุงุคู ู
ู derived class ุชุงููุ ุงูููุน ุฏู ู
ู ุงููุฑุงุซุฉ ุจูุชุณู
ู Multilevel Inheritance.
ูู ููู class ุงุณู
ู Aุ ูู
ู class A ุจููุฑุซ class Bุ ูู
ู class B ุจููุฑุซ class Cุ ูุงูููุน ุฏู ู
ู ุงููุฑุงุซุฉ ุจูุชุณู
ู Multilevel Inheritance.
Hierarchical Inheritance
ูู
ุง ุฃูุชุฑ ู
ู derived class ุจูุชู
ุฅูุดุงุคูู
ู
ู base class ูุงุญุฏุ ุงูููุน ุฏู ุจูุชุณู
ู Hierarchical inheritance.
ูู ุนูุฏู class ุงุณู
ู Aุ ูู
ู ุงูู class ุฏู ุจููุฑุซ ุฃูุชุฑ ู
ู class ุชุงููุ ูุนูู class B ุจููุฑุซ ู
ููุ ูclass C ุจููุฑุซ ู
ููุ ููู
ุงู class D ุจููุฑุซ ู
ูู. ูู
ุง ูููู ููู ุฃูุชุฑ ู
ู child class ุจููุฑุซูุง ู
ู Single Base Class ูุงุญุฏุ ุงูููุน ุฏู ู
ู ุงููุฑุงุซุฉ ุจูุชุณู
ู Hierarchical Inheritance.
Multiple Inheritance
ูู
ุง derived class ุจูุชู
ุฅูุดุงุคู ู
ู ุฃูุชุฑ ู
ู base class ูุงุญุฏุ ุงูููุน ุฏู ู
ู ุงููุฑุงุซุฉ ุจูุชุณู
ู multiple inheritance.
ูู ููู classes ุฒู A ูBุ ูู
ู ุงูุงุชููู ุฏูู ุจููุฑุซ class Cุ ูุงูููุน ุฏู ู
ู ุงููุฑุงุซุฉ ุจูุชุณู
ู Multiple Inheritance. ูุนูู ูู
ุง class ูุงุญุฏ ูููู ุนูุฏู ุฃูุชุฑ ู
ู parent class.
Hybrid Inheritance
ุงูู Hybrid Inheritance ูู ู
ุฒูุฌ ู
ู ุฃู ููุน ู
ู ุงูู Single, Hierarchical, ู Multilevel inheritances.
ูู ููู ุงุชููู subclasses ุฒู B ูC ุจููุฑุซูุง ู
ู class A (ูุฏู Hierarchical inheritance). ูุจุนุฏูู ู
ู B ูCุ ููู class ุชุงูู ุงุณู
ู D ุจููุฑุซ ู
ููู
ูู
ุง ุงูุงุชููู.
ุฏู ูุนุชุจุฑ ู
ุฒูุฌ ู
ู hierarchical inheritance ู
ู ูููุ ู multiple inheritance ู
ู ุชุญุช (D ุจููุฑุซ ู
ู B ูC). ูู
ุงูุ ู
ู A ูู B ูู
ู B ูู D (ูู ุงูุชุฑุถูุง ุงูุชุณูุณู ุฏู)ุ ุฏู ูุนุชุจุฑ Multi-level Inheritance. ูู ุงูููุน ุฏู ู
ู ุงููุฑุงุซุฉุ ุงูุฎุตุงุฆุต ุจุชุงุนุฉ base class A ุจุชุธูุฑ ูู class D ุนู ุทุฑูู class B ู class C.
ุงูุชุตููู ุงููู ูุงุช ุฏู ู
ุจูู ุนูู C++.
Types of Inheritances in C#
ูู ุจุตูุช ุนูู Single, Multilevel, ู Hierarchicalุ ูุชูุงูููู
ุดุจู ุจุนุถ ุฌุฏูุง. ูู ุฃู ูุญุธุฉุ ุจูููู ูููู
immediate parent class (ุฃุจ ู
ุจุงุดุฑ) ูุงุญุฏ ุจุณ.
ููู ูู ุจุตูุช ุนูู Multiple ู Hybridุ ุจูููู ูููู
ุฃูุชุฑ ู
ู immediate parent class ูุงุญุฏ ููู child class.
ุนุดุงู ูุฏูุ ููุฏุฑ ูุตูู ุงูุฎู
ุณ ูุฆุงุช ุงููู ููู ุฏู ุจุดูู ุฃูุณุน ูููุนูู ุฃุณุงุณููู ุจูุงุกู ุนูู ุนุฏุฏ ุงูู immediate parent class ูุงูุชุงูู:
- Single Inheritance (ุจูุดู
ู
Single,Multilevel, ูHierarchical) - Multiple Inheritance (ุจูุดู
ู
MultipleูHybrid)
Single Inheritance in C#
ูู ุฃู class ุนูุฏู immediate parent class ูุงุญุฏ ุจุณุ ุจูุณู
ูู Single Inheritance ูู C#.
ู
ุซูุงูุ ูุงู
immediate parent class ุนูุฏ Cุ ูุงุญุฏ ุจุณ ููู B. ููุงู
immediate parent class ุนูุฏ Bุ ูุงุญุฏ ุจุณ ููู A.
Multiple Inheritance in C#
ูู class ุนูุฏู ุฃูุชุฑ ู
ู immediate parent class ูุงุญุฏุ ุจูุณู
ูู Multiple Inheritance ูู C#.
ู
ุซูุงูุ class C ุนูุฏู ุฃูุชุฑ ู
ู immediate parent class ูุงุญุฏ ุงููู ูู
ุง A ู Bุ ูุจุงูุชุงูู ุฏู Multiple Inheritance.
ูุงูููุทุฉ ุงููู ูุงุฒู
ุชูุชูุฑูุง ูู ุนุฏุฏ ุงูู immediate Parent classes ุงููู ุงูู child class ุจูู
ุชูููุง. ูู ูุงุญุฏุ ูุจูู Single Inheritance. ูู ุฃูุชุฑ ู
ู ูุงุญุฏุ ูุจูู multiple inheritance.
Example to Understand Single Inheritance in C#
using System;
namespace InheritanceDemo
{
public class Program
{
static void Main()
{
// Creating object of Child class and
// invoke the methods of Parent and Child classes
Cuboid obj = new Cuboid(2, 4, 6);
Console.WriteLine($"Volume is : {obj.Volume()}");
Console.WriteLine($"Area is : {obj.Area()}");
Console.WriteLine($"Perimeter is : {obj.Perimeter()}");
Console.ReadKey();
}
}
// Parent class
public class Rectangle
{
public int length;
public int breadth;
public int Area()
{
return length * breadth;
}
public int Perimeter()
{
return 2 * (length + breadth);
}
}
// Child Class
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 : 48
Area is : 8
Perimeter is : 12
Example to Understand Multiple Inheritance in C#
using System;
namespace InheritanceDemo
{
public class Program
{
static void Main()
{
// Creating object of Child class and
// invoke the methods of Parent classes and Child class
SmartPhone obj = new SmartPhone(); ;
obj.GetPhoneModel();
obj.GetCameraDetails();
obj.GetDetails();
Console.ReadKey();
}
}
// Parent Class 1
class Phone
{
public void GetPhoneModel()
{
Console.WriteLine("Redmi Note 5 Pro");
}
}
// Parent class 2
class Camera
{
public void GetCameraDetails()
{
Console.WriteLine("24 Mega Pixel Camera");
}
}
// Child Class derived from more than one Parent class
class SmartPhone : Phone, Camera
{
public void GetDetails()
{
Console.WriteLine("Its a RedMi Smart Phone");
}
}
}Output:
ุงูู Output: ููุญุตู Compile Time Error.
ู
ูุงุญุธุฉ: ุงูุชุนุงู
ู ู
ุน ุงูุชุนููุฏ ุงููู ุจูุญุตู ุจุณุจุจ ุงูู multiple inheritance ุตุนุจ ุฌุฏูุง. ุนุดุงู ูุฏูุ ูู ู
ุด ู
ุฏุนูู
ูู .NET ู
ุน ุงูู class ูู
ู
ูู ูุชุนู
ู ุจุงูู interfaces. ููุชููู
ุนู ุงูู
ูููู
ุฏู ุจุงูุชูุตูู ูู ู
ูุงูุงุชูุง ุนู ุงูู Multiple Inheritance.
Classification of Inheritance in C#
ุงูู C#.NET ุตููู ุงูู inheritance ููุฆุชููุ ููู
ุง:
- Implementation inheritance: ูู
ุง
classุจููุฑุซ ู ูclassุชุงููุ ุฏู ุจูุชุณู ูimplementation inheritance. - Interface inheritance: ูู
ุง
classุจููุฑุซ ู ูinterfaceุ ุฏู ุจูุชุณู ูinterface inheritance.