Inheritance
Inheritance
o Is the fundamental concept in OOP (object-oriented
programming) which allows one class (the derived class) to
inherit members (methods and properties) from another class
(the base class).
o Inheritance is the key feature in OOP.
Consider the illustration
Types of Inheritance
1.Single-level inheritance.
In single inheritance this occurs when a class (child or
derived class) inherits from only one base class (parent class).
Example;
code
• using System;
• public class Animal
• {
• public void eat() {
• Console.WriteLine("Eating..."); }
• }
• public class Dog: Animal
• {
• public void bark() {
• Console.WriteLine("Barking...");
• }
• }
• class Program
• {
• public static void Main()
• {
• Dog d1 = new Dog();
• d1.eat();
• d1.bark(); }
• }
output
Continue……..
2. Multilevel Inheritance
• In multilevel inheritance, a class is derived from another
derived class, forming a chain of inheritance.
• Example
• Dog inherits from Mammal, and Mammal inherits from
Animal. The Dog class can access members from both
Mammal and Animal.
code
• class Animal // Base class
• { • class Dog : Mammal // Derived class 2
• public void Eat() • {
• { • public void Bark()
• Console.WriteLine("This animal eats • {
food."); • Console.WriteLine("The dog barks.");
• } • }
• } • }
• •
• class Program
• class Mammal : Animal // Derived class 1
• {
• {
• static void Main()
• public void Walk()
• {
• {
• Dog myDog = new Dog();
• Console.WriteLine("This mammal • myDog.Eat(); // Inherited from Animal
walks.");
• myDog.Walk(); // Inherited from Mammal
• }
• myDog.Bark(); // Defined in Dog
• } • }
• • }
output
Continue……..
3. Hierarchical Inheritance
• In hierarchical inheritance, multiple derived classes inherit
from a single base class.
Example
The Dog and Cat classes both inherit the Eat method from the
Animal class.
Code
• using System;
• class Cat : Animal // Derived class 2
• • {
• class Animal // Base class • public void Meow()
• { • {
• public void Eat() • Console.WriteLine("The cat meows.");
• { • }
• Console.WriteLine("This animal eats food.");
• }
• }
•
• }
• class Program
•
• class Dog : Animal // Derived class 1
• {
• { • static void Main()
• public void Bark() • {
• { • Dog myDog = new Dog();
• Console.WriteLine("The dog barks."); • myDog.Eat(); // Inherited from Animal
• } • myDog.Bark(); // Defined in Dog
• }
•
• • Cat myCat = new Cat();
• }
• myCat.Eat(); // Inherited from Animal
• }
•
• myCat.Meow(); // Defined in Cat
Output
Continue…..
4. Multiple Inheritance (Not Supported Directly)
• C# does not support multiple inheritance (where a class
inherits from more than one class) to avoid ambiguity
(commonly known as the "diamond problem"). However,
multiple inheritance can be achieved using interfaces
example.
• The Bird class implements both the IFlyable and ISwimmable
interfaces.
code
• using System;
• • public void Swim()
• interface IFlyable
• {
• {
• Console.WriteLine("The bird is
• void Fly(); swimming.");
• }
• }
•
• }
• interface ISwimmable
• {
•
• void Swim(); • class Program
• } • {
• • static void Main()
• class Bird : IFlyable, ISwimmable // Multiple • {
inheritance via interfaces
• {
• Bird myBird = new Bird();
• public void Fly() • myBird.Fly();
• { • myBird.Swim();
• Console.WriteLine("The bird is flying."); • }
• } • }
Output
Continue……
5. Hybrid Inheritance (Not Supported Directly)
• Hybrid inheritance is a combination of two or more types of
inheritance (e.g., hierarchical + multiple inheritance). Since C# does
not support multiple inheritance directly, hybrid inheritance is not
natively supported. However, it can still be implemented using
interfaces.
example
• Hybrid inheritance is simulated by combining class inheritance (Bird
inherits from Animal) with interface implementation (IFlyable).
code
• using System; • public void Fly()
• class Animal // Base class • {
• { • Console.WriteLine("The bird is flying.");
• public void Eat()
• }
• {
• }
• Console.WriteLine("This animal eats food.");
•
• }
• class Program
• }
• interface IFlyable
• {
• {
• static void Main()
• void Fly(); • {
• } • Bird myBird = new Bird();
• class Bird : Animal, IFlyable // Hybrid inheritance • myBird.Eat(); // Inherited from Animal
using a base class and interface • myBird.Fly(); // Implemented from
• { IFlyable
• } •
output
Continue….
6. Single Class Inheritance with Sealed Keyword
• In C#, you can prevent inheritance by marking a class as
sealed.
• example
• The sealed keyword prevents any class from inheriting the
Animal class.
Code
• using System;
• • class Program
• sealed class Animal // Sealed class • {
• { • static void Main()
• public void Eat()
• {
• {
• Console.WriteLine("This animal eats • Animal myAnimal = new
food."); Animal();
• } • myAnimal.Eat();
• }
• }
•
• // class Dog : Animal // ERROR: Cannot inherit
• }
from a sealed class
• // {}
Output
Importance Of Inheritence
The following are importance of inheritance in c# programming.
• Code re-usability.
Inheritance allows a derived class to reuse the properties and methods of its
base class, reducing the need to rewrite code.
• Method overriding.
Method overriding allows a derived class to provide a specific implementation
of a method already defined in the base class using the virtual and override
keywords.
• Organized class hierarchy.
Inheritance helps organize classes into a hierarchical structure, categorizing
them based on shared behavior and specialization