From the course: Learning C#

Inheritance - C# Tutorial

From the course: Learning C#

Inheritance

- [Instructor] As I said at the beginning of the course, C# is an object-oriented language which means that we can build class hierarchies using inheritance. In this video, we're going to use inheritance to expand the Book example that we've been using to represent other types of publications. So here, in the Inheritance folder, inside Classes in the Start folder, let's open up the Book class and we'll start here. So let's imagine that we want to create another type of publication called a Magazine. So one of the ways that we could do that is by creating a new Magazine class which I have here, and as you see in the editor, we could, within that class, define properties such as the publisher, the pagecount, and the price. And if we look over at the Book class, we can see that we have some similar fields. We've got the pagecount and the price. So we've created a problem, here. We've duplicated these fields and if we ever decide to update or change or class definitions, we're going to have to modify multiple places in the code which could lead to bugs. So to avoid this, we're going to create a parent class that holds the common code and then use subclasses for the more specific data. So in this file here, Publication, we're going to create a new class called Publication. And as you can see, it contains properties for the PageCount, the Price, and the Name. And also notice that the Publication class uses the explicit setter of the Name property to check and catch the case where the given string is empty and it throws an exception. So this is a good use of properties. You can perform error checking when values are set. So this will prevent the Name property from being set to an empty string. So now that we have our Publication class, we need to change Book and Magazine to be subclasses of Publication. So let me collapse that. So let's do Book, first. So I need to define Book as a subclass of Publication and the way that I do that is by putting a colon after the class name, and then the name of the superclass or the parent class which is Publication. And then I need to modify my constructor so that the base class gets initialized along with the subclass. And the way that I do that is by putting a colon and then calling the base keyword along with the name, pagecount, and price. And now, I can remove the redundant code that's in my Book class. So I can get rid of pagecount and price because Publication already has those. And then I'll make the same changes to Magazine. So Magazine will now inherit from Publication. And I'll also initialize the base class. So that's going to be name, pagecount, and price. And I'll get rid of these two fields that I don't need anymore. So I'm also going to add a method to the Publication class and I'm going to add a method that can be overridden by subclasses called GetDescription. So I'm going to define this as a public virtual method and it's going to return a string and it's going to be called GetDescription. And it's going to return an interpolated string that contains the Name and PageCount pages. So now, let's override this function in the Book class to further customize it. And because that method was declared virtual I'm going to use public override string GetDescription. And in this case, I'm going to return an interpolated string and it's going to say Name by Author and it is PageCount pages. And we're going to leave this out of the Magazine class. We'll simply just leave the Magazine class as it is. So now, let's test our code. And here in the program code, you can see that I have some code that creates a new Book and a new Magazine, and that I print out the Name and Author or Publisher, in the case of the Magazine, in each one. And then let's try uncommenting this code that tries to set the Name property to the empty string. And remember, that should throw an exception. So let's go ahead and open this up in our integrated terminal and let's run this. And, oh whoops, in Publication.cs it's capital C, PageCount. There we go. Let's try this again. And you can see that sure enough in the program file the Name and Author, and Name and Publisher gets printed and then we get the Name cannot be blank exception 'cause we tried to make the Name empty. Okay, so that's working. So let's go ahead and comment that code back. And now, let's uncomment this code that calls the GetDescription function on each of the objects, the Book and the Magazine. So now let's run this again. And you can see that for the Book, we get the customized output along with the Price. And for the Magazine, it's the default version which we did not override and we get the correct Price for each one. So using classes, we were able to de-duplicate our code and make things more organized and C#'s object-oriented features made it all pretty easy.

Contents