blog

Home / DeveloperSection / Blogs / Importance of the Inheritance

Importance of the Inheritance

Royce Roy2018 23-Jul-2016

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
{
    classSampleParentInhertiance
    {
        string str = string.Empty;
        public SampleParentInhertiance()
        {
            Console.WriteLine("Call Parent class the construtor");
        }
        public SampleParentInhertiance(string name)
        {
            str = name;
            Console.WriteLine("str");
 
        }
        publicint add(int x, int y)
        {
            return x + y;
        }
        publicdouble mul(double a, double b)
        {
            return a * b;
        }
        publicvoid 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
{
    classSampleInheritanceChild:SampleParentInhertiance
    {
        public SampleInheritanceChild()
        {
            Console.WriteLine("Call child class constructor");
        }
        public SampleInheritanceChild(string str2)
        {
            Console.WriteLine("Child class constructor call2");
        }
        publicint 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
{
    classSampleInheritanceTest
    {
        staticvoid Main(string[] args)
        {
            SampleInheritanceChild sc = newSampleInheritanceChild();
            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

Updated 16-Mar-2018

Leave Comment

Comments

Liked By