Understanding Inheritance in C# – Part 2

Title

Understanding Inheritance in C# – Part 2

Introduction

In my first article, Understanding Inheritance in C# – Part 1, we saw what is inheritance, and we look into its first type Single Inheritance.
In this post I’ll discuss about its second type Hierarchical Inheritance.

Hierarchical Inheritance

Hierarchical

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

This is the type of inheritance in which there are multiple classes derived from one base class. This type of inheritance is used when there is a requirement of one class feature that is needed in multiple classes.

Class file

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

namespace ConsoleApplication1
{
    class Person  //base class
    {
        private string name = "";
        private long phone;

        public void readData()
        {
            Console.Write("Enter Name: ");
            name = Console.ReadLine();
            Console.Write("Enter Phone Number: ");
            phone = Int64.Parse(Console.ReadLine());
        }

        public void showData()
        {
            Console.WriteLine("Name of the person is: " + name);
            Console.WriteLine("Phone number of the person is: " + phone);
        }
    }

    class Student : Person  //derived class 1
    {
        private long roll_no;
        private string course = "";
        new public void readData()
        {
            base.readData();  /* To Access readData() method of base (parent) class */
            Console.Write("Enter Roll Number: ");
            roll_no = Int64.Parse(Console.ReadLine());
            Console.Write("Enter Course Name: ");
            course = Console.ReadLine();
        }

        new public void showData()
        {
            base.showData();  /* To Access readData() method of base (parent) class */
            Console.WriteLine("Roll number is: " + roll_no);
            Console.WriteLine("Course is: " + course);
        }
    }

    class Teacher : Person   //derived class 2
    {
        private string dep_name = "";
        private string qualification = "";

        new public void readData()
        {
            base.readData();
            Console.Write("Enter Department Name: ");
            dep_name = Console.ReadLine();
            Console.Write("Enter Qualification: ");
            qualification = Console.ReadLine();
        }

        new public void showData()
        {
            base.showData();
            Console.WriteLine("Department name is: " + dep_name);
            Console.WriteLine("Qualification is: " + qualification);
        }
    }
}

 

Main Class

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            /*Enter Student Details*/
            Console.WriteLine("****Enter Student Details****");
            Console.WriteLine();
            Student s1 = new Student();
            s1.readData();
            Console.WriteLine();
            s1.showData();

            Console.WriteLine();
            Console.WriteLine("****Enter Teacher Details****");
            Console.WriteLine();
            /*Enter Teacher Details*/

            Teacher t1 = new Teacher();
            t1.readData();
            Console.WriteLine();
            t1.showData();
        }
    }
}

Output

Hierarchical result


JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Related Articles

Powered by Paras Babbar · Designed by Paras Babbar