Practical Inheritance

Hi Friends,

In this section, we will talk about practical implementation of Inheritance. Inheritance basically explains IS-A relationship. We will see the same in a moment. Below, I have created simple Doctor class which inherits from DoctorInfo.

Now, the 1st thing which I would like to do with this class is to create a constructor as shown below. 3rd But, for this I need to create one constructor in DoctorInfo class as well.Below is the implementation for the same.

Once, this is done, then our Doctor class with base class constructor will look like

Now, in the main program, I can go ahead and create new Doctor object as shown below. You can see here, it is prompting for base class constructor here. 4th With this my main program looks like this.

Here, I have not called ToString() method explicitly as this is implemented in DoctorInfo and since writeLine method calls ToString(); Hence, it will automatically implement that method.

With this when I will run the program, then it will produce the following output.

5th

Now, let us go ahead and create one method at both the places in Doctor and DoctorInfo as shown below.

and

Now, in the below screenshot, you will notice that compiler has raised one warning.

6th

So, basically compiler is suggesting, if hiding is intended then use new keyword. Therefore, when we put new keyword before the same as shown below, then warning will not come.

And, this will work fine. But, under certain conditions, this won’t work.

7th

This works fine as as DoctorInfo got populated with DoctorInfo and Doctor with Doctor. However, let us suppose the case.

Here, I am creating a variable of DoctorInfo but creating an object of Doctor. But, the problem with this approach is, when we invoke Information() method with this object, it will print different result.

8th

Here, I was expecting Doctor Assigned message rather and that’s because of new keyword which hides the implementation. In order to overcome this scenario, we need to make use of virtual keyword. Virtual says that we expect this method to be overridden in derived class. Below is the minor change.

and

With the above change in place, and no change in main program. This will print the correct result.

9th

Now, let’s verify the same against collection.

and, this will print the exact expected result.

10th

I hope you would have enjoyed this segment. We will see more inn coming section. Till then stay tuned and Happy Coding. I have also shared the code in github. Download here:- https://github.com/rahulsahay19/csharp-Basics

Thanks,
Rahul Sahay
Happy Coding

2 thoughts on “Practical Inheritance

  1. Your example is reeeeally unfortunate. How is a Doctor a DoctorInfo? that is not even the same kind of stuff. A better example would have been Person and Doctor, since all doctors are people, but how is a doctor a DoctorInfo? I really dislike this example, it gives bad example for people who are trying to learn basics of OOP and inheritance.

    • I can understand your point. But, this is just around generic one. I also understand that you are trying to relate this with IS-A relationship. That’s fine. But, I don’t always relate the same with a Noun, because when it comes to Enterprise level coding, you need to generalize the stuffs and you won’t be having the flexibility of noun or actors always.

Comments are closed.