how to use inheritance in C-#(SHARP) application

The most important reason to use object oriented programming (OOP's) is that , it's supports INHERITANCE. Inheritance is the property in which through a class derived properties from another class. A class that's inherit the properties of another class is called a child or derived class , whereas , the class from which a child class inherit is called as base or parent class. A parent class is the higher level in class hierarchy.

INHERITANCE IS OF FOUR TYPE , WHICH ARE AS FOLLOW:

single inheritance: In which t , there is only one base class or one derived class. This means that a derived class inherit the properties from single base class.

Hierarchical inheritance: In which , multiple derived class are inherit from single base class.

Multilevel inheritance: In which , a child class is derived from a base class , which is turned is derived from another class.

Multiple inheritance: In which , a child class derived from multiple base class.

showing the code how to impement inheritance in C# application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace inheritance.cs
{
    public class baseclass
    {
        public int datamember;
        public void baseclassmode()
        {
            Console.WriteLine("i am a base class method()");
        }
    }
        public class derivedclass : baseclass
        {
            public void derivedclassmethod()
            {
                Console.WriteLine("i am a derived class method()");
            }
        }
    class Program
    {
        static void Main(string[] args)
        {
            // create a base class object..
            Console.WriteLine("i am accessing base class object..");
            baseclass bc = new baseclass();
            bc.datamember = 1;
            bc.baseclassmode();
            Console.WriteLine("=====================================");
            // create a derived class object..
            Console.WriteLine("i am accessing derived class object..");
            derivedclass dc = new derivedclass();
            dc.datamember = 2;
            dc.baseclassmode();
            dc.derivedclassmethod();
            Console.WriteLine("press enter to quit..");
            Console.ReadKey();
        }
    }

}


\
Want to know more about inheritance click here ...

0 comments: