Inheritance is a mechanism of sharing the members among the classes. Inheritance means taking an existing class and adding functionality by deriving a new class from it. The class you start with is called the base class, and the new class you create is called the derived class.
When you derive a class from another class, the new class gets all the functionality of the base class plus whatever new features you add. You can add data members and functions to the new class, but you cannot remove anything from what the base class offers.
In C# inheritance may be implemented in different combinations as illustrated in figure and they include:
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritances
4. Hierarchical Inheritance
The Multiple inheritances do not directly implemented by C#. But we can implement the concept of multiple inheritances using interface
Derived class definition
A derived class can be defined with speCifying its relationship with the base class. The syntax and general form of derived class is:
Class derived-class-name: base-class-name { members of class }
In this method, you start with the class keyword followed by a name from your class. On the right side of the name of your class, you must type the ‘:’ operator, followed by the name of the class that will serve as parent. Of course, the Base class must have been defined; that is, the compiler must be able to find its definition.
For Example:
Class Circle: shape { Members of class }
Where Shape class is the name of an existing class and Circle class is the name of derived class.
Here is an example program to understand the concept of inheritance.
We’ll be covering the following topics in this tutorial:
Single inheritance
//single Inheritance
using System; class A { public void welcome() { Console.writeLine("welcome to C#"); } } class B : A //class B is derived by class A { public void Hello() { Console.writeLine("Hello Sir"); } } class single { public static void Main() { B obj = new B(); Obj.welcome(); Obj.Hello(); } }
OUTPUT:
Welcome to C#
Hello Sir
//base class member function
//derived class member function
Multilevel Inheritance
using System; class A { public void welcome() { console.writeLine("welcome to c#"); } } class B : A //class B is derived by class A { public void Hello() { Console.writeLine("Hello Sir"); } } class C : B //class C is derived by class B { console.writeLine("How are You ?"); } public void HOWRU() {} class single { public static void Main() { c obj = new C(); obj.welcome(); // super class member function obj.Hello(); // base class member function Obj.HOWRU(); // own member function } }
OUTPUT:
Welcome to C#
Hello Sir
How are You?
Hierarchical Inheritance
using System; class A { public void welcome() { Console.writeLine("welcome to c#"); } } class B : A //class B is derived by class A { public void Hello() { console.writeLine("Hello sir"); } } class C : A //class C is derived by class A { public void HOWRU() { console.writeLine("How are You ?"); } } class single { public static void Main() { B obj1 = new B(); C Obj2 = new c(); obj1.welcome(); obj1.Hello(); obj2.welcome(); obj2.HOWRU(); } }
OUTPUT:
Welcome to C#
Hello Sir
Welcome to C#
How are You?
Class Modifiers
When implementing inheritance, it is important to understand how establish accessibility level for our classes and their members. Class modifier is used to decide which parts of the system can create class objects.
Basically there are four different-optional-class modifiers. These are :
• public
• internal
• protected
• private
These are used to specify the access levels of the types defined by the classes. The following different access levels can be specified with above four modifiers:
• protected internal
• New
• Abstract
• sealed
Public
The ‘public’ keyword identifies a type as fully accessible to all other types. This is the implicit accessibility of enumeration members and interface members.
Internal
If a class is declared as ‘internal’, the type it defines is accessible only to types within the same assembly (a self-contained ‘unit of packaging’ containing code, metadata etc.), This is the default access level of non-nested classes.
Protected
If a class is declared as ‘protected’, its type is accessible by a containing type and any type that inherits from this containing type. This modifier should only be used for internal classes (i.e. classes declared within other classes).
Private
Where a class is declared as ‘private’, access to the type it defines is limited to a containing type only. This modifier should only be used for internal classes (ie. classes declared within other classes).
Protected internal
The permissions allowed by this access level are those allowed by the ‘protected’ level plus those allowed by the ‘internal’ level. The access level is thus more liberal than its parts taken individually. This modifier should only be used for internal classes (ie. Classes declared within other classes).
New
The ‘new’ keyword can be used for ‘nested’ classes. A nested class is one that is defined in the body of another class; it is in most ways identical to a class defined in the normal way, but its access level cannot be more liberal than that of the class in which it is defined. A nested class should be declared using the ‘new’ keyword just in case it has the same name as (and thus overrides) an inherited type.
Abstract
A class declared as ‘abstract’ cannot itself be instanced – it is designed only to be a base class for inheritance.
Sealed
A class declared as ‘sealed’ cannot be inherited from other classes. It is an error to use a sealed class as a base class. Use the sealed modifier in a class declaration to prevent inheritance of the class.
Abstract Classes
In C#, you can create a class whose role is only meant to provide fundamental characteristics for other classes. This type of class cannot be used to declare object of the class. Such a class is referred to as abstract. Therefore, an abstract class can be created only to serve as a parent class for others. To create an abstract class, write the abstract keyword to the left of its name. Here is an example:
abstract class Ba11 { protected int Typeofsport; protected string Dimensions; }
The important features of abstract class are:
• The abstract class may contain the one or more abstract methods
• The abstract class cannot be instantiate in the other classes
• If you are inheriting the abstract class in other class, you should implement all the abstract methods in the derived class. ‘
The sample program of abstract class.
using System; abstract class First { protected string s; public abstract void SayHello(); } class Second: First { public override void sayHello() { s = "Hello Wor1d" ; console.writeLine("{O}",s); } } class, mainclass { public static void Main() { Second S = new second(); s.sayHello(); } } A = x; b = y; } public int sum() { return ( a + b ); } public int mul() { return ( a * b ); } } class MYlnterface { public static void Main() { calculate cal = new calculate(5,10); Add A = (Add) cal; // casting Console.writeLine(“Sum: " + A.sum ()); Multiply M = (Multiply) cal; console.writeLine(“Multiplication :" + M.mul()); } }
OUTPUT:
Sum : 15
Multiplication : 50
Multiple implementation of an interface
using System; interface calculate { public compute( int x, int y); } class Add { public int compute() { return ( a + b ); } } class Multiply { public int compute() { return ( a * b ); } } class Newlnterface { public static void Main() { Add A = new Add(); Multiply M =new Multiply(); calculate cal; cal = A as calculate; //casting console.writeLine("Sum : " + cal.compute()); cal = M as calculate; //casting console.writeLine("Multiplication :" + cal.compute()); } }
OUTPUT:
Sum: 15
Multiplication: 50