Home > DeveloperSection > Blogs > Importance of the Inheritance

Importance of the Inheritance


OOPS OOPS  Inheritance 
Ratings:
1 Comment(s)
 390  View(s)
Rate this:

Importance of the Inheritance


In object oriented programming inheritance most important concepts of the programming language which is very useful and easy to use.

When to use: You can reuse the code whenever needed from the existing class, like method you can inherit and use it. You want to global change by base class.

 

For Example:

Create the class name as SampleInheritanceParent

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace OopsConcepts

{

    class SampleParentInhertiance

    {

        string str = string.Empty;

        public SampleParentInhertiance()

        {

            Console.WriteLine("Call Parent class the construtor");

        }

        public SampleParentInhertiance(string name)

        {

            str = name;

            Console.WriteLine("str");

 

        }

        public int add(int x, int y)

        {

            return x + y;

        }

        public double mul(double a, double b)

        {

            return a * b;

        }

        public void display()

        {

            Console.WriteLine("call display method");

        }

 

    }

}

 

Create the another class name as SampleInheritanceChild

Note: If you notice that we can inherit the SampleInheritanceChild: SampleParentInhertiance

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace OopsConcepts

{

    class SampleInheritanceChild:SampleParentInhertiance

    {

        public SampleInheritanceChild()

        {

            Console.WriteLine("Call child class constructor");

        }

        public SampleInheritanceChild(string str2)

        {

            Console.WriteLine("Child class constructor call2");

        }

        public int sub(int w,int z)

        {

       

            return w-z;

           

        }

 

    }

}

 

In last create another class with main method name as SampleInheritanceTest and create object and call it

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace OoopsConcepts

{

    class SampleInheritanceTest

    {

        static void Main(string[] args)

        {

            SampleInheritanceChild sc = new SampleInheritanceChild();

            Console.WriteLine(sc.sub(10, 5));

            Console.WriteLine(sc.mul(15, 5));

            Console.WriteLine(sc.add(25, 5));

            sc.display();

           // Console.WriteLine(sc.display(1));

 

            Console.ReadLine();

        }

    }

}

 

Output:

 

Call Parent class the constructor

Call child class constructor

5

75

30

Call display method

 

 

 

Why are use Interface

 

Interface most powerful programming tool because they set to separate definition from their implementation

 

Ø  Interface are suitable in which when we unrelated object provide different functionalities.

Ø  Interface is most reliable from base class because you can define a single implementation that can be implement multiple interface


Hi Buddy

By Manish Sahu on   4 months ago
Thanks for posting useful information related to inheritance. It is very helpful post. One of the key benefits of inheritance is to minimize the amount of duplicate code in an application by sharing common code amongst several subclasses.


Don't want to miss updates? Please click the below button!

Follow MindStick