Sample program of inheritance using c#.

Inheritance: The most important reason to use OOP is to make reusability of Code and eliminate the redundant code. That can be done by one of the OOP concept i.e inheritance. Inheritance supports reusability by defining a class and then use that class again and again.Inheritance supports a class hierarchy where Base class and Derived class exist, e.g. Animals is the [Base Class] and Dog is the [Derived Class].Derived Class inherits the base class. Also Derived Class can inherits all the member of Base Class.

This is your Code for Inheritance Program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritancedemo
{
public class Baseclass 
{
public int datamember;
public void baseclassmethod()
        {
            Console.WriteLine(“This is baseclass method”);
        }
    }
public class DerivedClass : Baseclass
    {
public void derivedclassmethod()
        {
            Console.WriteLine(“This is our derivedclassmethod”);
        }
    }
class Program
    {
static void Main(string[] args)
        {
//create base class object
            Baseclass bc = new Baseclass();
            bc.datamember=1;
            bc.baseclassmethod();
//create derived class object
            DerivedClass dc = new DerivedClass();
            dc.datamember = 2;
            dc.derivedclassmethod();
            dc.baseclassmethod();
            Console.ReadKey();
        }
    }
}

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*