top
April flash sale

Search

C# Tutorial

Inheritance allows a class to inherit the functionality of another class. The original class is known as the base class and the class which inherits the functionality of the base class is the derived class. Inheritance provides code reusability and implementation speedup.Base Class and Derived ClassInheritance implements a base class and a derived class. The original class from which the functionality is inherited is known as the base class and the class which inherits the functionality is known as the derived class.A program that demonstrates base class and derived class is given as follows:Source Code: Program that demonstrates base class and derived class in C#using System; namespace InheritanceDemo {     class A     {         public int a;         public A()         {            a = 5;         }     }     class B: A     {         public int b;         public B()         {             b = 20;         }         public int getSum()         {             return a + b;         }     }    class Test    {      static void Main(string[] args)      {          B obj = new B();         Console.WriteLine("Sum = {0}", obj.getSum());      }    } }The output of the above program is as follows:Sum = 25Now let us understand the above program.The class A is the base class. It contains a variable a and a default constructor which initializes the value of a to 5. The code snippet for this is given as follows:class A     {         public int a;         public A()         {            a = 5;         }     }The class B is the derived class and it derives A publically. It contains a variable b and a default constructor which initializes the value of b to 20. It also has a method getSum() that returns the sum of a and b. The code snippet for this is given as follows:class B: A     {         public int b;         public B()         {             b = 20;         }         public int getSum()         {             return a + b;         }     }The main() function contains an object of class B. It also prints the sum of a and b by calling the getSum() method. The code snippet for this is given as follows: static void Main(string[] args)      {         B obj = new B();         Console.WriteLine("Sum = {0}", obj.getSum());      }Single Level InheritanceSingle Level Inheritance involves a single class inheriting from another class. There is only one base class and one derived class.A program that demonstrates single level inheritance is given as follows:Source Code: Program that demonstrates single level inheritance in C#using System; namespace InheritanceDemo {     class A     {         public int a;         public A()         {            a = 10;         }     }     class B: A     {         public int b;         public B()         {             b = 7;         }     }    class Test    {      static void Main(string[] args)      {         Console.WriteLine("Single level inheritance");         B obj = new B();         Console.WriteLine("a = {0}",obj.a);         Console.WriteLine("b = {0}",obj.b);      }    } }The output of the above program is as follows:Single level inheritance a = 10 b = 7Now let us understand the above program.The class A is the base class. It contains a variable a and a default constructor which initializes the value of a to 10. The code snippet for this is given as follows: class A     {         public int a;         public A()         {            a = 10;         }     }The class B is the derived class and it derives A publically. It contains a variable b and a default constructor which initializes the value of b to 7. The code snippet for this is given as follows: class B: A     {         public int b;         public B()         {             b = 7;         }     }The main() function contains an object of class B. It prints the values of a and b by using the object obj. The code snippet for this is given as follows:static void Main(string[] args)      {         Console.WriteLine("Single level inheritance");         B obj = new B();         Console.WriteLine("a = {0}",obj.a);         Console.WriteLine("b = {0}",obj.b);      }Hierarchical InheritanceHierarchical inheritance involves multiple classes inheriting from a single base class. This is quite useful if the features of the base class are required in multiple classes.A program that demonstrates hierarchical inheritance is given as follows:Source Code: Program that demonstrates hierarchical inheritance in C#using System; namespace InheritanceDemo {     class A     {         public int a;         public A()         {            a = 10;         }     }     class B: A     {         public int b;         public B()         {             b = 7;         }     }      class C: A     {         public int c;         public C()         {             c = 23;         }     }    class Test    {      static void Main(string[] args)      {         Console.WriteLine("Hierarchical inheritance");         B obj1 = new B();         C obj2 = new C();         Console.WriteLine("Using class B object (obj1)");         Console.WriteLine("a = {0}",obj1.a);         Console.WriteLine("b = {0}",obj1.b);         Console.WriteLine("Using class C object (obj2)");         Console.WriteLine("a = {0}",obj2.a);         Console.WriteLine("c = {0}",obj2.c);      }    } }The output of the above program is as follows:Hierarchical inheritance Using class B object (obj1) a = 10 b = 7 Using class C object (obj2) a = 10 c = 23Now let us understand the above program.The class A is the base class. It contains a variable a and a default constructor which initializes the value of a to 10. The code snippet for this is given as follows:class A     {         public int a;         public A()         {            a = 10;         }     }Both the classes B and C are the derived classes and they derive A publically. Class B contains a variable b and a default constructor which initializes the value of b to 7. Class C contains a variable c and a default constructor which initializes the value of c to 23. The code snippet for this is given as follows:class B: A     {         public int b;         public B()         {             b = 7;         }     }      class C: A     {         public int c;         public C()         {             c = 23;         }     }The main() function contains an object of class B and class C i.e. obj1 and obj2 respectively. Then the values of a and b are printed by using the object obj1 and the values of a and c are printed using the object obj2. The code snippet for this is given as follows:static void Main(string[] args)      {         Console.WriteLine("Hierarchical inheritance");         B obj1 = new B();         C obj2 = new C();         Console.WriteLine("Using class B object (obj1)");         Console.WriteLine("a = {0}",obj1.a);         Console.WriteLine("b = {0}",obj1.b);         Console.WriteLine("Using class C object (obj2)");         Console.WriteLine("a = {0}",obj2.a);         Console.WriteLine("c = {0}",obj2.c);      }Multi-Level InheritanceMulti-level inheritance involves a class inheriting from a derived class. This derived class may have inherited from the base class or another derived class.A program that demonstrates multi level inheritance is given as follows:Source Code: Program that demonstrates multi level inheritance in C#using System; namespace InheritanceDemo {     class A     {         public int a;         public A()         {            a = 10;         }     }     class B: A     {         public int b;         public B()         {             b = 7;         }     }      class C: B     {         public int c;         public C()         {             c = 23;         }     }    class Test    {      static void Main(string[] args)      {         Console.WriteLine("Multi Level inheritance");         C obj = new C();         Console.WriteLine("a = {0}",obj.a);         Console.WriteLine("b = {0}",obj.b);         Console.WriteLine("c = {0}",obj.c);      }    } }The output of the above program is as follows:Multi Level inheritance a = 10 b = 7 c = 23Now let us understand the above program.The class A is the base class. It contains a variable a and a default constructor which initializes the value of a to 10. The code snippet for this is given as follows: class A     {         public int a;         public A()         {            a = 10;         }     }The class B is the derived class and it derives A publically. It contains a variable b and a default constructor which initializes the value of b to 7. The code snippet for this is given as follows:class B: A     {         public int b;         public B()         {             b = 7;         }     }The class C is the derived class and it derives B publically. It contains a variable c and a default constructor which initializes the value of c to 23. The code snippet for this is given as follows: class C: B     {         public int c;         public C()         {             c = 23;         }     }The main() function contains an object of class C. It prints the values of a, b and c by using the object obj. The code snippet for this is given as follows:static void Main(string[] args)      {         Console.WriteLine("Multi Level inheritance");         C obj = new C();         Console.WriteLine("a = {0}",obj.a);         Console.WriteLine("b = {0}",obj.b);         Console.WriteLine("c = {0}",obj.c);      }Multiple Inheritance Using InterfacesMultiple inheritance of classes is not supported by C#. So interfaces are used to solve this problem and implement multiple inheritance.A program that demonstrates multiple inheritance using interfaces is given as follows:Source Code: Program that demonstrates multiple inheritance using interfaces in C#using System; namespace InterfaceDemo {    public interface IA    {      void puta();    }    public interface IB    {      void putb();    }    public class C: IA, IB    {        private int a;        private int b;        public C(int x, int y)        {            a = x;            b = y;        }        public void puta()        {            Console.WriteLine("a = {0}", a);        }        public void putb()        {            Console.WriteLine("b = {0}", b);        }    }    class Test    {      static void Main(string[] args)      {         C obj = new C(17, 4);         obj.puta();         obj.putb();      }    } }The output of the above program is as follows:a = 17 b = 4Now let us understand the above program.The interface IA contains the declaration for the method puta() and the interface IB contains the declaration for the method putb().The code snippet for this is as follows:  public interface IA    {      void puta();    }    public interface IB    {      void putb();    }The class C inherits the interfaces IA and IB. This demonstrates multiple inheritance using interfaces. Class C contains 2 data variables a and b. A parameterized constructor initializes the values of a and b. Then the values of a and b are displayed using the methods puta() and putb() respectively. The code snippet for this is as follows:public class C: IA, IB    {        private int a;        private int b;        public C(int x, int y)        {            a = x;            b = y;        }        public void puta()        {            Console.WriteLine("a = {0}", a);        }        public void putb()        {            Console.WriteLine("b = {0}", b);        }    }The main() method contains the object of class C. Then the object obj is used to call the methods puta() and putb() that display the values of a and b respectively. The code snippet for this is as follows:static void Main(string[] args)      {         C obj = new C(17, 4);         obj.puta();         obj.putb();      }
logo

C# Tutorial

Inheritance in C#

Inheritance allows a class to inherit the functionality of another class. The original class is known as the base class and the class which inherits the functionality of the base class is the derived class. Inheritance provides code reusability and implementation speedup.

Base Class and Derived Class

Inheritance implements a base class and a derived class. The original class from which the functionality is inherited is known as the base class and the class which inherits the functionality is known as the derived class.

A program that demonstrates base class and derived class is given as follows:

Source Code: Program that demonstrates base class and derived class in C#

using System;
namespace InheritanceDemo
{
    class A
    {
        public int a;
        public A()
        {
           a = 5;
        }
    }
    class B: A
    {
        public int b;
        public B()
        {
            b = 20;
        }
        public int getSum()
        {
            return a + b;
        }
    }
   class Test
   {
     static void Main(string[] args)
     {
         B obj = new B();
        Console.WriteLine("Sum = {0}", obj.getSum());
     }
   }
}

The output of the above program is as follows:

Sum = 25

Now let us understand the above program.

The class A is the base class. It contains a variable a and a default constructor which initializes the value of a to 5. The code snippet for this is given as follows:

class A
    {
        public int a;
        public A()
        {
           a = 5;
        }
    }

The class B is the derived class and it derives A publically. It contains a variable b and a default constructor which initializes the value of b to 20. It also has a method getSum() that returns the sum of a and b. The code snippet for this is given as follows:

class B: A
    {
        public int b;
        public B()
        {
            b = 20;
        }
        public int getSum()
        {
            return a + b;
        }
    }

The main() function contains an object of class B. It also prints the sum of a and b by calling the getSum() method. The code snippet for this is given as follows:

 static void Main(string[] args)
     {
        B obj = new B();
        Console.WriteLine("Sum = {0}", obj.getSum());
     }

Single Level Inheritance

Single Level Inheritance involves a single class inheriting from another class. There is only one base class and one derived class.

A program that demonstrates single level inheritance is given as follows:

Source Code: Program that demonstrates single level inheritance in C#

using System;
namespace InheritanceDemo
{
    class A
    {
        public int a;
        public A()
        {
           a = 10;
        }
    }
    class B: A
    {
        public int b;
        public B()
        {
            b = 7;
        }
    }
   class Test
   {
     static void Main(string[] args)
     {
        Console.WriteLine("Single level inheritance");
        B obj = new B();
        Console.WriteLine("a = {0}",obj.a);
        Console.WriteLine("b = {0}",obj.b);
     }
   }
}

The output of the above program is as follows:

Single level inheritance
a = 10
b = 7

Now let us understand the above program.

The class A is the base class. It contains a variable a and a default constructor which initializes the value of a to 10. The code snippet for this is given as follows:

 class A
    {
        public int a;
        public A()
        {
           a = 10;
        }
    }

The class B is the derived class and it derives A publically. It contains a variable b and a default constructor which initializes the value of b to 7. The code snippet for this is given as follows:

 class B: A
    {
        public int b;
        public B()
        {
            b = 7;
        }
    }

The main() function contains an object of class B. It prints the values of a and b by using the object obj. The code snippet for this is given as follows:

static void Main(string[] args)
     {
        Console.WriteLine("Single level inheritance");
        B obj = new B();
        Console.WriteLine("a = {0}",obj.a);
        Console.WriteLine("b = {0}",obj.b);
     }

Hierarchical Inheritance

Hierarchical inheritance involves multiple classes inheriting from a single base class. This is quite useful if the features of the base class are required in multiple classes.

A program that demonstrates hierarchical inheritance is given as follows:

Source Code: Program that demonstrates hierarchical inheritance in C#

using System;
namespace InheritanceDemo
{
    class A
    {
        public int a;
        public A()
        {
           a = 10;
        }
    }
    class B: A
    {
        public int b;
        public B()
        {
            b = 7;
        }
    }
     class C: A
    {
        public int c;
        public C()
        {
            c = 23;
        }
    }
   class Test
   {
     static void Main(string[] args)
     {
        Console.WriteLine("Hierarchical inheritance");
        B obj1 = new B();
        C obj2 = new C();
        Console.WriteLine("Using class B object (obj1)");
        Console.WriteLine("a = {0}",obj1.a);
        Console.WriteLine("b = {0}",obj1.b);
        Console.WriteLine("Using class C object (obj2)");
        Console.WriteLine("a = {0}",obj2.a);
        Console.WriteLine("c = {0}",obj2.c);
     }
   }
}

The output of the above program is as follows:

Hierarchical inheritance
Using class B object (obj1)
a = 10
b = 7

Using class C object (obj2)
a = 10
c = 23

Now let us understand the above program.

The class A is the base class. It contains a variable a and a default constructor which initializes the value of a to 10. The code snippet for this is given as follows:

class A
    {
        public int a;
        public A()
        {
           a = 10;
        }
    }

Both the classes B and C are the derived classes and they derive A publically. Class B contains a variable b and a default constructor which initializes the value of b to 7. Class C contains a variable c and a default constructor which initializes the value of c to 23. The code snippet for this is given as follows:

class B: A
    {
        public int b;
        public B()
        {
            b = 7;
        }
    }
     class C: A
    {
        public int c;
        public C()
        {
            c = 23;
        }
    }

The main() function contains an object of class B and class C i.e. obj1 and obj2 respectively. Then the values of a and b are printed by using the object obj1 and the values of a and c are printed using the object obj2. The code snippet for this is given as follows:

static void Main(string[] args)
     {
        Console.WriteLine("Hierarchical inheritance");
        B obj1 = new B();
        C obj2 = new C();
        Console.WriteLine("Using class B object (obj1)");
        Console.WriteLine("a = {0}",obj1.a);
        Console.WriteLine("b = {0}",obj1.b);
        Console.WriteLine("Using class C object (obj2)");
        Console.WriteLine("a = {0}",obj2.a);
        Console.WriteLine("c = {0}",obj2.c);
     }

Multi-Level Inheritance

Multi-level inheritance involves a class inheriting from a derived class. This derived class may have inherited from the base class or another derived class.

A program that demonstrates multi level inheritance is given as follows:

Source Code: Program that demonstrates multi level inheritance in C#

using System;
namespace InheritanceDemo
{
    class A
    {
        public int a;
        public A()
        {
           a = 10;
        }
    }
    class B: A
    {
        public int b;
        public B()
        {
            b = 7;
        }
    }
     class C: B
    {
        public int c;
        public C()
        {
            c = 23;
        }
    }
   class Test
   {
     static void Main(string[] args)
     {
        Console.WriteLine("Multi Level inheritance");
        C obj = new C();
        Console.WriteLine("a = {0}",obj.a);
        Console.WriteLine("b = {0}",obj.b);
        Console.WriteLine("c = {0}",obj.c);
     }
   }
}

The output of the above program is as follows:

Multi Level inheritance
a = 10
b = 7
c = 23

Now let us understand the above program.

The class A is the base class. It contains a variable a and a default constructor which initializes the value of a to 10. The code snippet for this is given as follows:

 class A
    {
        public int a;
        public A()
        {
           a = 10;
        }
    }

The class B is the derived class and it derives A publically. It contains a variable b and a default constructor which initializes the value of b to 7. The code snippet for this is given as follows:

class B: A
    {
        public int b;
        public B()
        {
            b = 7;
        }
    }

The class C is the derived class and it derives B publically. It contains a variable c and a default constructor which initializes the value of c to 23. The code snippet for this is given as follows:

 class C: B
    {
        public int c;
        public C()
        {
            c = 23;
        }
    }

The main() function contains an object of class C. It prints the values of a, b and c by using the object obj. The code snippet for this is given as follows:

static void Main(string[] args)
     {
        Console.WriteLine("Multi Level inheritance");
        C obj = new C();
        Console.WriteLine("a = {0}",obj.a);
        Console.WriteLine("b = {0}",obj.b);
        Console.WriteLine("c = {0}",obj.c);
     }

Multiple Inheritance Using Interfaces

Multiple inheritance of classes is not supported by C#. So interfaces are used to solve this problem and implement multiple inheritance.

A program that demonstrates multiple inheritance using interfaces is given as follows:

Source Code: Program that demonstrates multiple inheritance using interfaces in C#

using System;
namespace InterfaceDemo
{
   public interface IA
   {
     void puta();
   }
   public interface IB
   {
     void putb();
   }
   public class C: IA, IB
   {
       private int a;
       private int b;
       public C(int x, int y)
       {
           a = x;
           b = y;
       }
       public void puta()
       {
           Console.WriteLine("a = {0}", a);
       }
       public void putb()
       {
           Console.WriteLine("b = {0}", b);
       }
   }
   class Test
   {
     static void Main(string[] args)
     {
        C obj = new C(17, 4);
        obj.puta();
        obj.putb();
     }
   }
}

The output of the above program is as follows:

a = 17
b = 4

Now let us understand the above program.

The interface IA contains the declaration for the method puta() and the interface IB contains the declaration for the method putb().The code snippet for this is as follows:

  public interface IA
   {
     void puta();
   }
   public interface IB
   {
     void putb();
   }

The class C inherits the interfaces IA and IB. This demonstrates multiple inheritance using interfaces. Class C contains 2 data variables a and b. A parameterized constructor initializes the values of a and b. Then the values of a and b are displayed using the methods puta() and putb() respectively. The code snippet for this is as follows:

public class C: IA, IB
   {
       private int a;
       private int b;
       public C(int x, int y)
       {
           a = x;
           b = y;
       }
       public void puta()
       {
           Console.WriteLine("a = {0}", a);
       }
       public void putb()
       {
           Console.WriteLine("b = {0}", b);
       }
   }

The main() method contains the object of class C. Then the object obj is used to call the methods puta() and putb() that display the values of a and b respectively. The code snippet for this is as follows:

static void Main(string[] args)
     {
        C obj = new C(17, 4);
        obj.puta();
        obj.putb();
     }

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

austin faith

Avery good write-up. Please let me know what are the types of C# libraries used for AI development.

kariya arti

very satisfied!!

jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial