Please note, this is a STATIC archive of website technosmarter.com from July 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

C# Inheritance and types


Inheritance is the major important part of OOPS (object-oriented programming language ). If you are learned inheritance in C++ that means, it will be easy for you. Inheritance is a mechanism in C# programming is allowed to add new features with existing features.

As you know, a class can contain functions and properties with lots of functionality. Inheritance is a mechanism that inherits the existing class feature to the drive class.

 

Let's understand inheritance with an easy example.

Suppose that , we have a mobile-like Nokia 1100. Nokia 1100 has a lot of features like calling, messaging, gaming.
A developer wants to update this with new.
A developer updates the Nokia 1100 to Nokia 1108. The Nokia 1108 contains the features of Nokia 1100 and new features.
Suppose - Nokia 1100 features
Calling
Messaging
Gaming


A developer updates -

Nokia 1108 - Features - 
Calling - old Nokia 1100 feature inherited
Messaging
Gaming
50 short messages- New features along with old features of 1100.
Picture messaging etc.

The Nokia 1108 is used the features of Nokia 1100 and the new feature is called inheritance.

Nokia 1100 is a base (Super )
Nokia 1108 is a drive (Sub )


In C#, the drive class contains the features of the base class is called inheritance.

 

In the current time, we update the versions of the application using old features and create new features. We reuse the old features along with new features.


C# inheritance advantage -

The major advantage of inheritance is reusability. In C#, we create new class properties along with existing class properties.


Definition of C# Inheritance -

Inheritance is a process of defining a class in terms of another class, which makes it easier to create and maintain an application

In simple words -

In C#, inheritance is a mechanism that provides a feature of reusability to use existing features in the new class. In C# programming, the base class functionality comes into the derive class after inherited.



C# base class - A base class is a class that inherits into the drive class. It is known as the existing class.

C# Drive class- A drive class is known as the subclass. The drive class can inherit data and functions from multiple base classes or interfaces. It can observe all codes of base classes.



Inherit base class syntax in C#.


acess-specifier class base_class {
    // Data members 
}

class derived_class : base_class {
   //base class Data members 
          +
   //Data members 
}

Let's understand the inheritance with example.

C# inheritance: Single level inheritance example -


using System;  
   public class Nokia1100 
    {  
       
       public string msg="Message feature in Nokia 1100";
    public string call="Calling feature in Nokia 1100";
      public string game="Gaming feature in Nokia 1100";
       
   }  
   public class Nokia1108: Nokia1100
   {
   public string fsm="50 short messages feature in Nokia 1108(new)";
    public string picmsg="Picture messaging feature in Nokia 1108(new)";
   
   }  
   class TestInheritance{  
       public static void Main(string[] args)  
        {  
            Nokia1108 obj = new Nokia1108();  
   Console.WriteLine("Features of Nokia 1108--Full specification");  
   
            Console.WriteLine(obj.msg);  
              Console.WriteLine(obj.call);  
  
    Console.WriteLine(obj.game);  
  
    Console.WriteLine(obj.fsm);  
  
    Console.WriteLine(obj.picmsg);  
  
  
        }  
    }  

Execute the above example. It produces the result.

Features of Nokia 1108--Full specification
Message feature in Nokia 1100
Calling feature in Nokia 1100
Gaming feature in Nokia 1100
50 short messages feature in Nokia 1108(new)
Picture messaging feature in Nokia 1108(new)

In the above C# inheritance example, we inherited the Nokia 1100 features into Nokia 1108. The Nokia 1100 is a base class and Nokia 1108 is a drive class. It is an easy example to understand the inheritance concept in C#.
This is known as single level inheritance. In C# single level inheritance, a drive class can inherit the only a single base class.

Types of inheritance in C#-


There are many types of inheritance.

1. C# Supports - Single inheritance.


In the C# single inheritance, a derive class can inherit only one base class and a base class can be inherit only into on drive class. We discussed Single level inheritance in the above example.

2. C# Multilevel inheritance -


The C# supports multi-level inheritance. In this type of inheritance, a superclass inherits into a drive class and a drive class act as a base class for another class and inherits into another class.

Example -
Suppose that, we have three classes -
Class A
Class B
Class C

Class is a superclass that inherits into class B.

Class B acts as the base class for class C.

Class B inherits into class C.

C# multilevel inheritance syntax - 


acess-specifier class A {
    // Data members 
}
class B : A { 
   //Data members 
}
class C : B {
          
   //Data members 
}

3. C# Hierarchical Inheritance-


In C#, one class serve as the superclass for other drive classes. A superclass inherits into drive classes is known as Hierarchical Inheritance.

Example -

Class A
Class B
Class C
Class D

Class A is sever as super class for B, C,D drive class .
Class A inherits into B, C, D drive class.

C# Hierarchical Inheritance Syntax -


acess-specifier class A {
    // Data members 
}

class B: A {
    
          
   //Data members 
}
class C : A {
    
          
   //Data members 
}

class D: A {
    
          
   //Data members 
}

4. Multiple Inheritance-

C# does not support multiple inheritance. To achieve multiple inheritances in C#, you can use interface.


C# Hybrid Inheritance-


C# does not support multiple inheritances and hybrid inheritance using class. You can use interface to achieve multiple and Hybrid inheritance in C#.


The above information is related to types of inheritance in C#.

Let's create another example to inherit method of base class into drive class.


C# Inheritance example for methods -


using System;  
   public class Add 
    {  
       public int a ; 
      public  int b; 
       public int c; 
        
       public void Adition(int a , int b) {
         
           c=a+b; 
           Console.WriteLine("Addition of a and b ="+c);
         
       }  
   }  
   public class Subtract: Add  
   {  
       public void subs(int a , int b) 
       
       { 
          c=a-b; 
           Console.WriteLine("Subtraction of a and b  = "+c);
           
           
       }  
   }  
 class Calculator{  
       public static void Main(string[] args)  
        {  
           
           Subtract obj = new Subtract();  
            obj.Addition(90,70);
            obj.subs(90,70);
        }  
 }

Output 

  Addition of a and b =160
Subtraction of a and b  = 20


In the above C# example, derive class inherited bass class addition method into derive class with new subs method.


Please Share

Recommended Posts:-