Hierarchical Inheritance in C#
In C#, hierarchical inheritance is a type of inheritance where multiple derived classes inherit from a single base class. This enables code reuse and allows multiple subclasses to have a common set of functionalities from the parent class, while also defining their unique behaviours.
Key Characteristics of Hierarchical Inheritance
- A single base class is shared among multiple derived classes.
- Each derived class can access the base class's members (fields and methods), depending on the access modifiers.
- Promotes the DRY (Don't Repeat Yourself) principle by allowing code reuse.
- Each derived class can extend or override the base class's functionality as needed.
Technically speaking, it is a method of passing along attributes from a parent class to a base, child, or subclass while maintaining object-orientedness. The parent class, also known as the superclass, is the class from which the features are inherited. When a parent class is inherited by several subclasses, this is referred to as hierarchical inheritance. A form of inheritance called hierarchical inheritance occurs when multiple classes are inherited from a single parent or base class. Many properties of the parent class are also shared by the base class, particularly those that are shared by the parent class. A base class is the root of many classes. Having multiple children, each with their own set of characteristics acquired from their parents. For example, In the diagram below, class A acts as the base class(parent class) for the child classes B, C, and D.
Example 1: Animals
using System;
class Animal
{
public void Eat()
{
Console.WriteLine("This animal eats food.");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("The dog barks.");
}
}
class Cat : Animal
{
public void Meow()
{
Console.WriteLine("The cat meows.");
}
}
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
dog.Eat();
dog.Bark();
Cat cat = new Cat();
cat.Eat();
cat.Meow();
}
}
Example 2: Vehicles
using System;
class Vehicle
{
public void Start()
{
Console.WriteLine("The vehicle has started.");
}
}
class Car : Vehicle
{
public void Drive()
{
Console.WriteLine("The car is being driven.");
}
}
class Bike : Vehicle
{
public void Ride()
{
Console.WriteLine("The bike is being ridden.");
}
}
class Program
{
static void Main(string[] args)
{
Car car = new Car();
car.Start();
car.Drive();
Bike bike = new Bike();
bike.Start();
bike.Ride();
}
}
Example 3: Shapes
using System;
class Shape
{
public void DisplayShape()
{
Console.WriteLine("This is a shape.");
}
}
class Circle : Shape
{
public void DrawCircle()
{
Console.WriteLine("Drawing a circle.");
}
}
class Rectangle : Shape
{
public void DrawRectangle()
{
Console.WriteLine("Drawing a rectangle.");
}
}
class Program
{
static void Main(string[] args)
{
Circle circle = new Circle();
circle.DisplayShape();
circle.DrawCircle();
Rectangle rectangle = new Rectangle();
rectangle.DisplayShape();
rectangle.DrawRectangle();
}
}
This Content Sponsored by Buymote Shopping app BuyMote E-Shopping Application is One of the Online Shopping App Now Available on Play Store & App Store (Buymote E-Shopping) Click Below Link and Install Application: https://buymote.shop/links/0f5993744a9213079a6b53e8 Sponsor Content: #buymote #buymoteeshopping #buymoteonline #buymoteshopping #buymoteapplication
0 Comments