Inheritance is a mechanism where one class (derived class) acquires the properties and behaviors of another class (base class). C# supports several types of inheritance:
1. Single Inheritance
A derived class inherits from only one base class.
class Base {
public void Show() { }
}
class Derived : Base {
// inherits Show() from Base
}
2. Multilevel Inheritance
A class is derived from another derived class, forming a chain.
class GrandParent { }
class Parent : GrandParent { }
class Child : Parent { }
3. Hierarchical Inheritance
Multiple classes inherit from a single base class.
class Base { }
class Child1 : Base { }
class Child2 : Base { }
4. Multiple Inheritance (via Interfaces)
C# does not support multiple class inheritance directly, but multiple interfaces can be implemented.
interface IA { }
interface IB { }
class Derived : IA, IB { }
5. Hybrid Inheritance
A combination of two or more types of inheritance. In C#, achieved using interfaces along with classes.
interface IA { }
class Base { }
class Derived : Base, IA { }
Note: C# allows single, multilevel, hierarchical, and hybrid inheritance using interfaces, but not direct multiple class inheritance.
No comments:
Post a Comment