What is the Inheritance and why we need of inheritance?
Inheritance is like sharing and passing down traits from one thing to another. Imagine you have a toy robot with cool features. Now, you want to make a new robot that's similar to the first one, but with a few extra tricks. Instead of building everything from scratch, you can use inheritance.
In programming, inheritance is when you create a new class based on an existing class. This new class gets all the qualities of the old one, plus you can add or change things to make it special.
Imagine you have a "Vehicle" class that has properties like speed and color. Now you want to make a "Car" class. Instead of rewriting all the speed and color stuff, you can inherit from the "Vehicle" class. This means the "Car" class automatically has speed and color, and you can add more car-specific things to it.
Inheritance makes programming faster and organized. It helps you reuse code and build new things on top of existing things. Just like building a new robot that's awesome by starting with the cool features of the old one.
Here are some points to remember about inheritance in C#:
-
Inheritance is the important pillar of object oriented programming in which one class inherits the features of another class to access the members (properties and methods) of other class.
- The class which is inherited by another class is called super class, base class or parent class.
- The class which inherits the other class is known as sub class or derived class or extended class or child class.
- The derived class is allowed to add its own field and methods in addition of super class.
- In C#, colon (':') is the symbol used for inheritance.
The inheritance implements the IS-A relationship. For example 'Mammal' is a animal. 'Sheep' IS A mammal, hence sheep is also an animal.
Let's illustrate inheritance with an example using classes related to animals:
using System;
// Base class: Mammal
class Mammal
{
public void Breathe()
{
Console.WriteLine("Mammal is breathing");
}
}
// Derived class: Animal (inherits from Mammal)
class Animal : Mammal
{
public void Move()
{
Console.WriteLine("Animal is moving");
}
}
// Further derived class: Sheep (inherits from Animal)
class Sheep : Animal
{
public void Bleat()
{
Console.WriteLine("Sheep is bleating");
}
}
class Program
{
static void Main(string[] args)
{
// Create an instance of Sheep
Sheep mySheep = new Sheep();
// Use methods from different levels of inheritance
mySheep.Breathe(); // Method from Mammal
mySheep.Move(); // Method from Animal
mySheep.Bleat(); // Method from Sheep
}
}
In this example:
-
'Mammal' is the base class. It has a method 'Breathe()'.
- 'Animal' is derived from 'Mammal'. It has a method 'Move()' and inherits the 'Breathe()' method from 'Mammal'.
- 'Sheep' is further derived from 'Animal'. It has a method 'Bleat()' and inherits both the 'Move()' and 'Breathe()' methods from its parent classes.
When you create an instance of 'Sheep' and call methods on it, you can see how the inheritance works:
-
The 'Breathe()' method is inherited from 'Mammal'.
- The 'Move()' method is inherited from 'Animal'.
- The 'Bleat()' method is specific to the 'Sheep' class.
This way, you can see how the traits and behaviors of each class are inherited and built upon as you move down the inheritance hierarchy.
Why we use inheritance?
Inheritance is like sharing good stuff between classes. Imagine you're making a new app, and you need a class that does some things. But there's already a class that does similar things. Instead of making everything from scratch, you can use the old class as a starting point by inheriting from it. The old class is like a helpful friend, and your new class is like you learning from that friend.
This also makes things quicker because you don't need to write as much code. The old class already has some cool stuff, so you just need to add a little bit to make it do exactly what you want. It's like starting with a recipe that's almost perfect and just adding your secret ingredient.