Skip to content

INHERITANCE

Inheritance is a process in which an object of one class can inherit the properties of the other class. It provides code reusability and achieves run time polymorphism.

<access_specifier> class <base_class>
{
    //...do something
}

<access_specifier> class <derived_class>:<base_class>
{
    //...do something
}
using System;

namespace ClassUnderstanding
{
    class ShapeInput
    {
        public int length { get; set; }
        public int width { get; set; }

        public void setlength(int l)
        {
            length = l;
        }

        public void setWidth(int w)
        {
            width = w;
        }

    }

    //shape class is inheriting shapeinput 
    class Shape: ShapeInput
    {
        public int Area()
        {
            return (length * width);
        }
    }

    class Program
    { 
        static void Main(string[] args)
        {
            Shape shapeobject = new Shape(); //derived class object.

            shapeobject.setlength(10); //derived class object using base class methods
            shapeobject.setWidth(20);

            int finalvalue = shapeobject.Area();

            Console.WriteLine("area of rectangle = {0}",finalvalue);

            Console.ReadKey();
        }
    }
}

In the above code, we have seen how a derived class object can access all the members of a base class. Now let’s dive little deep, if we have constructors then how will the inheritance differ.

using System;

namespace ClassUnderstanding
{
    class StudentClass
    {
        public int BoysCount { get; set; }
        public int GirlsCount { get; set; }

        public StudentClass(int b, int g)
        {
            Console.WriteLine("Base Class is Created");
            BoysCount = b;
            GirlsCount = g;
        }

        public void Boys()
        {
            Console.WriteLine("No of Boys: {0}", BoysCount);
        }

        public void Girls()
        {
            Console.WriteLine("No of Girls: {0}", GirlsCount);
        }
    }

    class Students: StudentClass
    {
        public string StudentName { get; set; }
        public int StudentClass { get; set; }
        public string StudentGender { get; set; }

        //base class constructor needs to be initialized before the derived class
        public Students(int b, int g, string N, int C, string G): base(b,g)
        {
            Console.WriteLine("Derived Class is Created");
            StudentName = N;
            StudentClass = C;
            StudentGender = G;
        }

        public void Name()
        {
            Console.WriteLine("Student Name: {0}", StudentName);
        }

        public void Section()
        {
            Console.WriteLine("Student Class: {0}", StudentClass);
        }

        public void Gender()
        {
            Console.WriteLine("Student Gender: {0}", StudentGender);
        }
    }

    class Program
    { 
        static void Main(string[] args)
        {
            Students student = new Students(15, 10, "teja", 10, "Male");
            student.Name();
            student.Gender();
            student.Section();
            student.Boys();
            student.Girls();

            Console.ReadKey();
        }
    }
}

Some Important Points:

  1. Base Class Constructor must be initialized before the derived class constructor.
  2. C# doesn’t support multiple inheritances i.e A class cant inherit from multiple classes.
  3. Multiple Inheritance creates the diamond problem. So for implementing Multiple inheritances, we could use interface.
Published inC#

One Comment

  1. SMS SMS

    Thanks so much for the blog post.

Leave a Reply

Your email address will not be published. Required fields are marked *