Inheritance

 Def: The process of implementing the "Parent-Child" relationship between two or more classes.
 As a part of this "Inheritance" implementation, parent and child classes are to be implemented.
 Advantage: All the data members and methods of the parent class are accessible by child class. This is called as “Re-usability”. That means the parent class members are re-usable in the child class.
 The parent class can also be called as "Base" / "Super" class.
 The child class can also be called as "Derived" / “Sub” class.
Inheritance

Classification of Inheritance supported by C#:

1) Implementation Inheritance:
 This is commonly used inheritance.
 Whenever a class is derived from another class, it can be called as “Implementation Inheritance”.
 As said above, all of the members of the super class are adopted by sub class.

2) Interface Inheritance:
 This type of inheritance is taken from “Java”.
 Whenever a class is derived from an interface, it can be called as “Interface Inheritance”.
 The interface is similar to the class, but doesn’t contain the method definitions; it contains the method declarations only.
We discuss about the interfaces in-depth later.

Types of Inheritance:

1) Single Inheritance
 This is the simplest type of inheritance.
 An inheritance relationship, with only one super class and one sub class.
Inheritance
2) Hierarchical Inheritance
 An inheritance relationship, with only one super class and multiple sub classes.
Hierarchical
3) Multi-Level Inheritance
 An inheritance relationship, with only one super class and multiple sub classes, and also extended with another sub class from the first sub class.
 In other words, a class acts as super class and sub class simultaneously.
Multi-Level
4) Multiple Inheritance
 An inheritance relationship, with only multiple super classes and only one sub class.
 “Multiple Implementation Inheritance” is not supported by C#, but “Multiple Interface Inheritance” is supported by C#.
Multiple
5) Hybrid Inheritance
 An inheritance relationship that contains a combination of any other two types of inheritances.
Hybrid
 Ex:
 Multiple + Hierarchical Inheritance
 Multiple + Multi Level Inheritance etc.

Note: Other OOP languages like C++ support multiple inheritance. But C# and VB.NET doesn’t support multiple inheritance to avoid some practical problems while developing future GUI applications like windows forms applications, web sites etc.
But instead, C# and VB.NET supports “multiple interface inheritance”, because it doesn’t cause any practical problems.
Implementation steps for Inheritance

 Define the super class

class superclassname
{
//data members of super class
//methods of super class
}

 Define the sub class

class subclassname : superclassname
{
//data members of sub class
//methods of sub class
}

 Construct the object for sub class

SubClass obj = new SubClass();

 Invoke methods
obj.SuperClassMethod();
obj.SubClassMethod();

Example

 We start with an example:

class A
{
void Hello()
{
Console.WriteLine(“Hello, World!”);
}
}
class B:A
{
void Hai()
{
Console.WriteLine(“Hai to all!”);
}
}

 In the above example, we have two classes called “A” and “B”. The inheritance relationship has been established between these classes. Now, the “A” class is called as “Super” class and “B” class is called as “Sub” class. All the members of “A” class are exactly copied into “B” class. Finally, whenever you construct an object for the “B” class, you can access both “Hello()” method and “Hai()” method also from that object. This is what we learned up-to-now.
 But sometimes, some of the super class members may not be accessible directly from the sub class’s object. This kind of nature depends on the “visibility modifiers”, we use in the super class, while we declare the super class members.

Visibility Modifiers

 C# supports the following visibility modifiers.
Sl. No Visibility Modifier Description
1 private This is the default modifier. The item is visible only within the self class.
2 public The item is visible in any other classes. It offers un-limited access to the item.
3 protected The item is visible in the self class and in the derived classes.
4 internal The item is visible anywhere in the current assembly only.

A simple demo on inheritance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleInheritanceDemo
{
//super class
class Country
{
public void ShowCountry()
{
Console.WriteLine("This is India..");
}
}
//sub class
class State : Country
{
public void ShowState()
{
Console.WriteLine("This is Andhra Pradesh..");
}
}
class Program
{
static void Main(string[] args)
{
//create instance for sub class
State st = new State();
//access the members
st.ShowCountry();
st.ShowState();
Console.Read();
}
}
}
Multi-Level
: Demo on “protected” modifier

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleInheritanceDemo
{
//super class
class Country
{
protected void ShowCountry()
{
Console.WriteLine("This is India..");
}
}
//sub class
class State : Country
{
public void ShowState()
{
ShowCountry();
Console.WriteLine("This is Andhra Pradesh..");
}
}
class Program
{
static void Main(string[] args)
{
//create instance for sub class
State st = new State();
//access the members
st.ShowState();
Console.Read();
}
}
}

Multi-Level