Inheritance
The physical features of a child are similar to his/her
parents, because child having inherited features from his/her parents.
For Example:
Definition:
The creation of new class based on the attribute,
property and methods of existing class. The created class is derived class or
child class and the existing class is based or parent class.
It is the backbone of the OOP which allow reusability
of inherited attributes method etc.
For Example:
A vehicle class which has one attribute and one method,
as we know that each vehicle either 2 wheelers or 4 wheelers both have color
and speed.
In this way we can inherit two wheelers and four
wheelers with the vehicle class in order to use its attribute and method like
child uses home, laptop and car of its parents.
Purpose:
The purpose of inheritance is reusability which allows
us to use common methods attribute among classes without recreating them.
Inheritance is widely used for
- Generalization
- Specialization
- Extension
Generalization
By the help of inheritance we can implement
generalization, like we have seen vehicle class example where vehicle is
generalize for all type of vehicles (two, three, four wheelers etc.).
Specialization
By the help of inheritance we can implement
specialization, like bus, car class is specialized class, because when we talk
about four wheeler its means that we are talking generalized class which have attribute
and methods which are common for all four wheeler, but when we talk about
particular four wheeler like bus or car its means we are talking about
specialization.
Extension
By the help of inheritance we can extend the
functionalities of derived class by creating more methods and attribute that
are not present in the base class.
Syntax:
<Derive class name> :< base Class>
Inheritance in C# is done by using Colon.
Example
class Animal
{
public void Eat()
{
Console.WriteLine("All animals
are eating");
}
}
class Cat : Animal
{
public string color = "Black";
}
What
will happen when we make an object of Cat class?
Main method will be.
static void Main(string[] args)
{
Cat myCat = new Cat();
Console.WriteLine(myCat.color);
myCat.Eat();
Console.ReadLine();
}
Output:
Black
All animals
are eating.
Constructor Chaining
What will happen when we use default constructor in
both classes?
Example
class Animal
{
public Animal()
{
Console.WriteLine("hi, this is
Animal Constructor");
}
public void Eat()
{
Console.WriteLine("All animals
are eating");
}
}
class Cat:Animal
{
public string color = "black";
public Cat()
{
Console.WriteLine("hi, this is
Cat Constructor");
}
}
static void Main(string[] args)
{
Cat myCat = new Cat();
Console.WriteLine(myCat.color);
myCat.Eat();
Console.ReadLine();
}
Output:
Red area shows constructor of Animal class but as you
can see I have created Cat class object only, this type of functionality is called
constructor chaining.
After Animal class’s constructor all remaining code will
runs normally.
For
example
Consider all classes have constructor
- Vehicle constructor will be call
- Four wheelers class constructor will be call
- Then car constructor will be call
Base keyword
By the help of base keyword we can access base class (parent
class) methods and variables; we can also re-declare variables and methods in
derived class.
We cannot use base keyword for static methods in base
class.
Example:
class Animal
{
public Animal()
{
Console.WriteLine("hi, this is
Animal Constructor");
}
public void Eat()
{
Console.WriteLine("All animals
are eating");
}
}
class Cat:Animal
{
public string color = "black";
public void CatEat()
{
base.Eat();
Console.WriteLine("but, Cat
also eats");
}
}
static void Main(string[] args)
{
Cat myCat = new Cat();
myCat.CatEat();
Console.ReadLine();
}
Output:
hi, this is animal constructor
All animals are eating
But, Cat also eats
In the above example we created another method named
CatEat which calls parent class (base class) method (we can also use base
keyword for variables) and then its own code.
Constructor Inheritance
By the help of base keyword we can inherit construct of
derived class with the particular constructor of base class (like in case of
constructor overloading).
Example:
class Animal
{
public Animal()
{
Console.WriteLine("hi, this is
Animal default Constructor");
}
public Animal(string spec)
{
Console.WriteLine("hi, this is
{0} Animal Constructor",spec);
}
}
class Cat:Animal
{
public string color = "black";
public Cat():base("special")
{
Console.WriteLine("hi, this is
Cat Constructor");
}
}
static void Main(string[] args)
{
Cat myCat = new Cat();
Console.ReadLine();
}
Output:
Hi, this is
special Animal Constructor
Hi, this is Cat Constructor
Noticed that I have only create Cat Object but it Shows
Animal class Constructor which have one parameter.
When we create object of class, it first execute constructor
of a class, but in this case when compiler comes at constructor it find base
keyword with one parameter (string type) it go to base class and find
constructor with one parameter(string) and execute it and return back to the constructor
of derived class(Cat Class).