blog

Home / DeveloperSection / Blogs / Basic Concept of inheritance, abstract and interface

Basic Concept of inheritance, abstract and interface

Mark Devid1716 21-Sep-2016

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 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 and B is called Base class. 

Note: If we are not using Abstract method 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.

    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 the place where the designing or structure or blueprint of the program with variable is declared or define. It has a special feature 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'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();
        }
    }
}
 
 

Updated 16-Mar-2018

Leave Comment

Comments

Liked By