0% found this document useful (0 votes)
5 views

Inheritance in C#

The document explains various types of inheritance in C#, including Single, Multilevel, Hierarchical, Multiple (via Interfaces), and Hybrid inheritance. Each type is defined with syntax and examples demonstrating how classes and interfaces can inherit properties and methods. A summary table is also provided, indicating whether each inheritance type is supported in C# along with corresponding examples.

Uploaded by

devil289wl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Inheritance in C#

The document explains various types of inheritance in C#, including Single, Multilevel, Hierarchical, Multiple (via Interfaces), and Hybrid inheritance. Each type is defined with syntax and examples demonstrating how classes and interfaces can inherit properties and methods. A summary table is also provided, indicating whether each inheritance type is supported in C# along with corresponding examples.

Uploaded by

devil289wl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Inheritance in C#

Definition:
Inheritance is an object-oriented feature that allows a class (derived/child class) to acquire
members (fields, methods, etc.) from another class (base/parent class).

1. Single Inheritance

 One derived class inherits from one base class.


 Most basic and commonly used form of inheritance.

Syntax:

class BaseClass
{
// base members
}

class DerivedClass : BaseClass


{
// derived members
}

Example:

using System;

class Vehicle
{
public void Start()
{
Console.WriteLine("Vehicle is starting...");
}
}

class Car : Vehicle


{
public void Drive()
{
Console.WriteLine("Car is driving...");
}
}

class Program
{
static void Main()
{
Car myCar = new Car();
myCar.Start();
myCar.Drive();
}
}

Output:

Vehicle is starting...
Car is driving...

2. Multilevel Inheritance

 A class inherits from a derived class, forming a chain.


 Base → Intermediate → Derived

Syntax:

class A { }
class B : A { }
class C : B { }

Example:

using System;

class Person
{
public void Info()
{
Console.WriteLine("I am a person.");
}
}

class Employee : Person


{
public void Work()
{
Console.WriteLine("Employee is working.");
}
}

class Manager : Employee


{
public void Manage()
{
Console.WriteLine("Manager is managing the team.");
}
}

class Program
{
static void Main()
{
Manager m = new Manager();
m.Info();
m.Work();
m.Manage();
}
}

Output:

I am a person.
Employee is working.
Manager is managing the team.

3. Hierarchical Inheritance

 Multiple classes inherit from the same base class.


 Useful when multiple derived classes share common features.

Syntax:

class Base { }
class A : Base { }
class B : Base { }

Example:

using System;

class Animal
{
public void Eat()
{
Console.WriteLine("Animal is eating...");
}
}

class Dog : Animal


{
public void Bark()
{
Console.WriteLine("Dog is barking...");
}
}

class Cat : Animal


{
public void Meow()
{
Console.WriteLine("Cat is meowing...");
}
}

class Program
{
static void Main()
{
Dog d = new Dog();
d.Eat();
d.Bark();

Cat c = new Cat();


c.Eat();
c.Meow();
}
}

Output:

Animal is eating...
Dog is barking...
Animal is eating...
Cat is meowing...

4. Multiple Inheritance (via Interfaces)

 C# does not support multiple class inheritance, but it allows multiple interface
implementation.
 Used to achieve multiple behaviors without ambiguity.

Syntax:

interface A { }
interface B { }

class C : A, B { }

Example:

using System;

interface IReader
{
void Read();
}

interface IWriter
{
void Write();
}

class Author : IReader, IWriter


{
public void Read()
{
Console.WriteLine("Author is reading...");
}

public void Write()


{
Console.WriteLine("Author is writing...");
}
}

class Program
{
static void Main()
{
Author a = new Author();
a.Read();
a.Write();
}
}

Output:

Author is reading...
Author is writing...
5. Hybrid Inheritance (Using Interfaces)

 A combination of two or more types of inheritance.


 Not directly supported with classes but possible via classes + interfaces.

Syntax:

interface A { }
class B { }
class C : B, A { }

Example:

using System;

interface IWorker
{
void DoWork();
}

class Person
{
public void Talk()
{
Console.WriteLine("Person is talking...");
}
}

class Engineer : Person, IWorker


{
public void DoWork()
{
Console.WriteLine("Engineer is solving problems...");
}
}

class Program
{
static void Main()
{
Engineer e = new Engineer();
e.Talk();
e.DoWork();
}
}

Output:

Person is talking...
Engineer is solving problems...

Summary Table

Inheritance Supported in
Description Example Shown
Type C#?
One class inherits from one base
Single ✅ Yes Vehicle → Car
class
Derived class is inherited by Person → Employee →
Multilevel ✅ Yes
another derived class Manager
Multiple classes inherit from one
Hierarchical ✅ Yes Animal → Dog, Cat
base class
One class implements multiple ✅ Yes (via Author : IReader,
Multiple
interfaces interfaces) IWriter
Combination of class and interface Engineer : Person,
Hybrid ✅ Yes
inheritance IWorker

You might also like