---
title: "Basic Concept of inheritance, abstract and interface"  
description: "When there is a requirement of the method or member in new class and same method or member is already defined in another class then we can derive it t"  
author: "Mark Devid"  
published: 2016-09-21  
updated: 2018-03-16  
canonical: https://www.mindstick.com/blog/11209/basic-concept-of-inheritance-abstract-and-interface  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# Basic Concept of inheritance, abstract and interface

**Inheritance:**\

When there is a requirement of the method or member in new class and same method or member is already defined in another class then we can derive it to the new class. This phenomenon is called Inheritance in Oops.

It can be done by using the colon (:) after the new [class name](https://www.mindstick.com/forum/12871/how-to-get-class-name) and then we need to write the name of the class which is having method or member.

**Syntax Class A: B**

In technical term A is called [derived class](https://answers.mindstick.com/qa/30686/what-is-a-base-class-and-derived-class) and B is called [Base class](https://www.mindstick.com/blog/116/base-class-initilization).

Note: If we are not using [Abstract method](https://www.mindstick.com/articles/23305/abstract-methods-and-class-in-c-sharp) or Interface method, then we need to give the definition of the function in the base class.

**Here is the small program related to inheritance which helps you to understand the [concept of inheritance](https://www.mindstick.com/forum/158920/explain-the-concept-of-inheritance-in-c-sharp-and-provide-an-example).**

```
    using System;    using System.Collections.Generic;    using System.Linq;    using System.Text;    using System.Threading.Tasks;
    namespace ConsoleApplication4    {
        class p1        {

            public int a = 5;            protected int b = 6; //can't be call to main program because of access specifier.            private int c = 6; // can't be inherit to class p2 because access specifier.            public int fun3()
           {                return c * b * a;            }
        }
        class p2 : p1        {
            internal int d;            public int fun1()
            {

                d = a * b;
                return d;
            }
        }        class p4 : p2        {            public int fun2()            {
                return a * d;
            }
        }
        class Program        {

            static void Main(string[] args)            {
                p4 p3 = new p4();
                p3.a = 35;
                int e = p3.fun1();

                int f = p3.fun2();

                int g = p3.fun1();
                Console.WriteLine("E = {0} & f = {1} & g={2}", e, f, g);

                Console.ReadLine();

            }

        }

    }
```

##### Abstract Method:

An [Abstract class](https://www.mindstick.com/articles/1430/abstract-class) the place where the designing or structure or blueprint of the program with [variable is declared](https://www.mindstick.com/interview/23061/how-variable-is-declared-in-php) or define. It has a [special feature](https://answers.mindstick.com/qa/39394/the-concept-of-rule-of-law-is-a-special-feature-of-constitutional-system-of-which-country) that we can declare an Abstract function within it by using 'Abstract' keyword, definition will not defined in the base class but we need give the definition of abstract functions in another class by inherit it and we have to use the keyword 'Override' before the name of the function.

***Note:***

[If we don](https://answers.mindstick.com/qa/94523/why-do-we-need-to-update-the-whatsapp-app-what-if-we-don-t-update)'t use the abstract method, then we need to give the definition of the function in the abstract class.

In Abstract method we can define the function which is not abstract.

In abstract method we need to give the definition of each abstract functions in derived a class.

```
 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace ConsoleApplication4{    abstract class p1    {        public int a = 5;        protected
int b = 5;        public abstract int fun3();        public void fun1()        {            Console.WriteLine("MindStick");        }    }     class p2 : p1    {        internal
int d;        public override int fun3()        {            fun1();            d = a * b;            return
d;        }    }     class Program    {        static void Main(string[]
args)        {            p2 p3 = new
p2();            p3.a = 35;            int
e = p3.fun3();            Console.WriteLine("E = {0} ", e);            Console.ReadLine();        }    }}  
```

**Interface:**

Interface is used in the C# for declaring the method or member, which can be implemented, in the various classes. It is similar to the base class which can be inherited into the derived but it is multilayered and class inherit concept is single layer hence it is more accessible.

**Note: We can't define any function in the Interface.**

```
 using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConsoleApplication11{    interface Interface1    {        void
SampleMethod();    }     interface Interface2    {        void
SampleMethod2();    }    class interfacedemo : Interface1, Interface2    {        public void SampleMethod()        {            Console.WriteLine("Sample Method");        }         public void SampleMethod2()        {            Console.WriteLine("SampleMethod2");        }         static void Main()        {            interfacedemo obj = new interfacedemo();            obj.SampleMethod();            obj.SampleMethod2();            Console.ReadLine();        }    }}  
```

---

Original Source: https://www.mindstick.com/blog/11209/basic-concept-of-inheritance-abstract-and-interface

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
