Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C#

Learn step by step



Inheritance

Inheritance is a mechanism of acquiring the features and behaviors of a class by another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. Inheritance implements the IS-A relationship.

For example, mammal IS-A animal, dog IS-A mammal; Hence dog IS-A animal as well.

Advantages

  • Reduce code redundancy.
  • Provides code re-usability.
  • Reduces source code size and improves code readability.
  • Code is easy to manage and divided into parent and child classes.
  • Supports code extensibility by overriding the base class functionality within child classes.

Disadvantages

  • In Inheritance base class and child classes are tightly coupled. Hence If you change the code of parent class, it will get affects to the all the child classes.
  • In class hierarchy many data members remain unused and the memory allocated to them is not utilized. Hence affect performance of your program if you have not implemented inheritance correctly.

Different Types of Inheritance

OOPs supports the six types of inheritance as given below-

1.Single inheritance

When a single derived class is derived from single base class,then that inheritance is called as single inheritance

In single inheritance we have single base class that is inherited by the derived class. Here the derived class has all the features of the base class and can add some new features. The inheritance also depends on the access specifier that is used at the time of base class inheritance.

class Parent
{
body
}
class child : parent
{
body
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance
{
class Program
{
static void Main(string[] args)
{
dad d = new dad();
d.car();
son s = new son();
s.bike();
s.car();
Console.ReadKey();
}
class dad
{
public void car()
{
Console.WriteLine("car");
}
}
class son:dad
{
public void bike()
{
Console.WriteLine("bike");
}
}
}
}

Output

car
bike
car
2.Multi-level inheritance

Acquiring properties to a subchild class from the child class and also from the parent class. When a derived class is created from another derived class,then that inheritance is called as Multi level Inheritance

Syntax for C# multi level inheritance

class parent
{
body
}
class child : parent
{
body
}
class subchild : child
{
body
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance
{
class Program
{
static void Main(string[] args)
{
grandfather g=new grandfather();
g.helicopter();
dad d = new dad();
d.car();
d.helicopter();
son s = new son();
s.bike();
s.car();
s.helicopter();
Console.ReadKey();
}
class grandfather
{
public void helicopter()
{
Console.WriteLine("helicopter");
}
}
class dad:grandfather
{
public void car()
{
Console.WriteLine("car");
}
}
class son : dad
{
public void bike()
{
Console.WriteLine("bike");
}
}
}
}

Output

helicopter
car
helicopter
bike
car
helicopter
3.Multiple inheritance

One child with two parents is known as C# Multiple Inheritance Multiple Inheritance can contain only one derived class,two or more base classes. When a derived class is created from more than one base class then that inheritance is called as Multiple Inheritance Multiple Inheritance is not supported in c# .net because due to ambiguity or confusion problem.

Syntax for C# multi level inheritance

class parent
{
body
}
class child : parent
{
body
}
class subchild : child
{
body
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance
{
class Program
{
static void Main(string[] args)
{
grandfather g=new grandfather();
g.helicopter();
dad d = new dad();
d.car();
d.helicopter();
son s = new son();
s.bike();
s.car();
s.helicopter();
Console.ReadKey();
}
class grandfather
{
public void helicopter()
{
Console.WriteLine("helicopter");
}
}
class dad:grandfather
{
public void car()
{
Console.WriteLine("car");
}
}
class son : dad
{
public void bike()
{
Console.WriteLine("bike");
}
}
}
}

Output

helicopter
car
helicopter
bike
car
helicopter
4. Hierarchical inheritance

Single parent and multiple child are called as C# Hierarchical Inheritance In other words when more than one derived class are created from single base class is called C# Hierarchical Inheritance

Syntax for C# hierarchical inheritance:

class parent
{
body
}
class child1 : parent
{
body
}
class child2 : parent
{
body
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance
{
class Program
{
static void Main(string[] args)
{
grandfather g=new grandfather();
g.helicopter();
dad d = new dad();
d.helicopter();
d.car();
son s = new son();
s.helicopter();
s.bike();
Console.ReadKey();
}
class grandfather
{
public void helicopter()
{
Console.WriteLine("helicopter");
}
}
class dad:grandfather
{
public void car()
{
Console.WriteLine("car");
}
}
class son : grandfather
{
public void bike()
{
Console.WriteLine("bike");
}
}
}
}

Output

helicopter
helicopter
car
helicopter
bike
Multipath inheritance

In this inheritance, a derived class is created from another derived classes and the same base class of another derived classes. This inheritance is not supported by .NET Languages like C#, F# etc.

Hybrid inheritance

This is combination of more than one inheritance. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multipath inheritance or Hierarchical, Multilevel and Multiple inheritance.

Since .NET Languages like C#, F# etc. does not support multiple and multipath inheritance. Hence hybrid inheritance with a combination of multiple or multipath inheritance is not supported by .NET Languages.



Related Videos