What is Inheritance in OOP?

What is Inheritance in OOP?

What is inheritance in OOP? The question seems to be very simple and yes it is simple but Inheritance in programming is taking the properties and methods from another class is called inheritance. Finding the answer to What is inheritance in OOP sometimes difficult but I will try to make it very easy for you in this article. First, understand What is Inheritance.

What is Inheritance?

Just like we inherit a lot of properties and habits from our parents. When someone says you look and walk exactly like your grandpa, he is saying your father has inherited facial features and walking style from your grandpa and you have inherited those things from your father. This is called inheritance.

What-is-Inheritance-in-OOP

In programming whenever we want to extend the functionality of an existing class, we use inheritance.

Things to Remember When Using Inheritance

  1. Deriving a new class from an existing class is called Inheritance.
  2. The Existing class is called the base class.
  3. The New Class is called the derived class.
  4. Every member (property and function) of the existing class will be inherited into the derived class except private members.

In the above base and derived class terminology, some programmers like to use parent and child class terminology. They call a base class is a parent class and a derived class is a child class.

Where to use Inheritance

Suppose there is a class A which is created by some other programmer, you want to use the methods and properties of Class A but they do not fulfill your requirement. You wish you could add some extra properties or methods to Class A but unfortunately, the creator of Class A has not allowed anyone to make changes to the structure of the class.

The solution in this situation is Inheritance. You will create a new Class B and make class B a derived class of Class A. By doing this you will have all the functionalities of Class A and because you are the creator of Class B you can add as many properties or methods as you want.

How to Implement Inheritance in Coding

class TeacherType
    {
        private string status { get; set; }
        private bool permanent { get; set; }
        public void MakePermanent(bool permanent)
        {
            this.permanent = permanent;
        }
        public string GetStatus()
        {
            if (permanent == false)
            {
                status = "Ad Hoc";
            }
            else
            {
                status = "Permanent";
            }
            return status;
        }
    }
    class Person
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string Langauge { get; set; }
        public void Eat()
        {
            Console.WriteLine("This Person is Easting");
        }
        public void Sleep()
        {
            Console.WriteLine("This Person is sleeping");
        }
        public void GetDetails()
        {
            Console.WriteLine(Name);
            Console.WriteLine(Address);
            Console.WriteLine(Langauge);
        }
    }
    class Teacher : Person
    {
        public long Salary { get; set; }
        public string Specialization { get; set; }
        public string Teachertype { get; set; }
        public void Teach()
        {
            Console.WriteLine("This person is teaching");
        }
        public new void GetDetails()
        {
            Console.WriteLine(Name);
            Console.WriteLine(Address);
            Console.WriteLine(Langauge);
            Console.WriteLine(Salary);
            Console.WriteLine(Specialization);
            Console.WriteLine(Teachertype);
        }
    }
    class Student : Person
    {
        public int Standard { get; set; }
        public long RollNumber { get; set; }
        public string Stream { get; set; }
        public void WriteExam()
        {
            Console.WriteLine("Writing Exam");
        }
        public new void GetDetails()
        {
            Console.WriteLine(Name);
            Console.WriteLine(Address);
            Console.WriteLine(Langauge);
            Console.WriteLine(Standard);
            Console.WriteLine(RollNumber);
            Console.WriteLine(Stream);
        }
    }

As you can see in the code above, the Colon (:) is used to implement Inheritance in C#. You can see we have created a class named Person and defined the basic properties and functionalities of a person.

Then we have created two more classes named Teacher and Student. The Teacher class has its own properties and functionalities. The same goes for Student Class and its functionalities.

Because Teacher and Student, both the classes are derived from Person class. Both the Teacher and Student will have all the properties and methods of a person.

using System;
namespace Inheritance
{
    class TeacherType
    {
        private string status { get; set; }
        private bool permanent { get; set; }
        public void MakePermanent(bool permanent)
        {
            this.permanent = permanent;
        }
        public string GetStatus()
        {
            if (permanent == false)
            {
                status = "Ad Hoc";
            }
            else
            {
                status = "Permanent";
            }
            return status;
        }
    }
    class Person
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string Langauge { get; set; }
        public void Eat()
        {
            Console.WriteLine("This Person is Easting");
        }
        public void Sleep()
        {
            Console.WriteLine("This Person is sleeping");
        }
        public void GetDetails()
        {
            Console.WriteLine(Name);
            Console.WriteLine(Address);
            Console.WriteLine(Langauge);
        }
    }
    class Teacher : Person
    {
        public long Salary { get; set; }
        public string Specialization { get; set; }
        public string Teachertype { get; set; }
        public void Teach()
        {
            Console.WriteLine("This person is teaching");
        }
        public new void GetDetails()
        {
            Console.WriteLine(Name);
            Console.WriteLine(Address);
            Console.WriteLine(Langauge);
            Console.WriteLine(Salary);
            Console.WriteLine(Specialization);
            Console.WriteLine(Teachertype);
        }
    }
    class Student : Person
    {
        public int Standard { get; set; }
        public long RollNumber { get; set; }
        public string Stream { get; set; }
        public void WriteExam()
        {
            Console.WriteLine("Writing Exam");
        }
        public new void GetDetails()
        {
            Console.WriteLine(Name);
            Console.WriteLine(Address);
            Console.WriteLine(Langauge);
            Console.WriteLine(Standard);
            Console.WriteLine(RollNumber);
            Console.WriteLine(Stream);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student
            {
                Name = "Michael",
                Address = "New York",
                Langauge = "Spanish",
                Standard = 4,
                RollNumber = 10,
                Stream = "Science",
            };
            student.GetDetails();
            student.WriteExam();
            Console.WriteLine("*******************");
            TeacherType teacherType = new TeacherType();
            teacherType.MakePermanent(false);
            Teacher teacher = new Teacher()
            {
                Name = "John",
                Address = "London",
                Langauge = "English",
                Salary = 10000,
                Specialization = "Physics",
                Teachertype = teacherType.GetStatus()
            };
            teacher.GetDetails();
            teacher.Teach();
            Console.ReadLine();
        }
    }
}

Output

Explanation

Main Method

If you are a little bit aware of programming languages, you would know there is a point in your program where you execute your program. That point is the main method. Look at the screenshot of the same program below, I have highlighted the main method.

In this main method, we will be using all of our classes and their properties and methods.

You can see we have created the objects of Student and Teacher classes in our main method and initialized the properties as well for the respective classes.

Initializing Properties

Initialization in programming means we have set the values for the properties in the classes. See the screenshot below on how to initialize the properties.

Once the values are initialized, we have access to the GetDetails methods in the respective classes to print the details on the console panel.

Related Posts

We have three classes Person, Teacher, and Student. The three properties of a Person are Name, Address, and Language are inherited to the Teacher and the Student because both are a person.

The Two Methods of Person Eat and Sleep are also inherited to The Teacher and the Student class.

Notice the point that every class has a GetDetails method but when we call the GetDetails method, the local method gets called.

In a child-parent relationship, when the same name method is available in both the classes. The child method will be invoked by default. If you want to invoke the method in the parent class, there is another way to do it. We will talk about that in some other article.

I know you must be thinking about Protected Access Modifier. This access modifier was introduced to make a class member in-accessible outside the class and accessible to the child class at the same time. If there is a class that is not a child class then the protected Modifier member will not be accessible to that class.

Types of Inheritances

  1. Single Inheritance
  2. Hierarchical Inheritance
  3. Multi-Level Inheritance

Single Inheritance

When one class becomes the parent class of another class. This relationship is called Single Inheritance. Look at the diagram of Single Inheritance below.

The single-level Inheritance was implemented in the above example program where a person class was intended by a Student class.

Hierarchical Inheritance

When a Single class becomes the parent class of 2 or more classes, this relationship is called Hierarchical Inheritance.

Hierarchical Inheritance was used in the Student-Teacher example program above when the Person class was extended by two classes called Student Class and Teacher Class.

Multi-Level Inheritance

Let’s say there are three classes, Class A, Class B, and Class C. Class A is the parent class of Class B. Class B is the parent class of Class C. This relationship is called Multi-Level Inheritance.

This level of inheritance relationship can be achieved if we extend a class which already extends some other class. See the modified program below.

using System;

namespace Inheritance
{
    class TeacherType
    {
        private string status { get; set; }
        private bool permanent { get; set; }

        public void MakePermanent(bool permanent)
        {
            this.permanent = permanent;
        }


        public string GetStatus()
        {
            if (permanent == false)
            {
                status = "Ad Hoc";
            }
            else
            {
                status = "Permanent";
            }

            return status;
        }

    }

    class Principal : Teacher
    {
        public void PrincipalsTasks()
        {
            Console.WriteLine("Performing Principals Tasks");
        }
    }


    class Person
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string Langauge { get; set; }

        public void Eat()
        {
            Console.WriteLine("This Person is Easting");
        }

        public void Sleep()
        {
            Console.WriteLine("This Person is sleeping");
        }

        public void GetDetails()
        {
            Console.WriteLine(Name);
            Console.WriteLine(Address);
            Console.WriteLine(Langauge);
        }
    }

    class Teacher : Person
    {
        public long Salary { get; set; }
        public string Specialization { get; set; }
        public string Teachertype { get; set; }
        public void Teach()
        {
            Console.WriteLine("This person is teaching");
        }

        public new void GetDetails()
        {
            Console.WriteLine(Name);
            Console.WriteLine(Address);
            Console.WriteLine(Langauge);
            Console.WriteLine(Salary);
            Console.WriteLine(Specialization);
            Console.WriteLine(Teachertype);
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            TeacherType teacherType = new TeacherType();

            teacherType.MakePermanent(false);

            Teacher teacher = new Teacher()
            {

                Name = "Albert Pinto",
                Address = "London",
                Langauge = "English",
                Salary = 10000,
                Specialization = "Physics",
                Teachertype = teacherType.GetStatus()
            };

            teacher.GetDetails();
            teacher.Teach();
            Console.WriteLine("*********************");

            Principal principal = new Principal()
            {
                Name = "John Cena",
                Address = "USA",
                Langauge = "English",
                Salary = 100,
                Specialization = "Mathematics",
            };

            principal.PrincipalsTasks();
            principal.GetDetails();

            Console.ReadLine();
        }
    }
}

As you can see in the modified version of our student-teacher example, we have added another class called Principal Class that only has one method called PrincipalsTasks. Principal Class extends Teacher class. The Teacher class already extends Person class.

By following the above hierarchy we now have the Principal class which has all the public and non-private properties and functions of the Person class as well as the Teacher Class.

I hope you have found the asnwer to what is inheritance in OOP.

There are some other types of Inheritances are also available which are not supported by C# or Java.

  1. Multiple Inheritance (Not Supported by C# and Java, It is possible by using Interface)
  2. Hybrid Inheritance (Not Supported by C# and Java)

Let us not discuss what is not supported by C# or Java and move to the next topic which is Polymorphism.

See you next time guys. Good Luck!