Part 3 Why and when should we use an abstract class

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is part three of c-sharp interview questions video series in this video will answer this interview question why and when should we use an abstract class this interview question could also be asked in a slightly different way give an example of where we could use an abstract class interfaces and abstract classes or complex subjects if we answer the questions related to them properly in an interview chances are the interviewer may get very good impression about you and in the subsequent questions even if we make slight mistakes the interviewer may be happy to forgive you at least in the interviews that I do I would definitely ask questions related to interfaces and abstract classes so in this video let's try an answer this question why use abstract classes with an example let's say in an organization there are two types of employees full-time employee and contract employee first let's design full-time employee class so here I have a blank console application so first let's go ahead and add a class file and let's call it full-time employee and to speed things up I have already provided the implementation for full-time employee class so let's copy and paste that class definition within this file so if you look at this class this is very straightforward we've got four auto implemented properties here ID first name last name an annual salary and we have got two methods get a full name which is going to return the full name of the employee by concatenating the first name and last name with the space in between them and we have another method here which is going to compute monthly salary so how do we compute the monthly salary divide annual salary by twelve so very straightforward class now let's go ahead and design the other type of employee which is contract employee so let's add another class file to this project and let's call this contract employee dot C s and to speed things up I'm going to copy this definition and paste it within contract employee a and change it as required let's make this class public okay so even if it's contract employee he is still going to have ID first name last name but a contract employee will not have annual salary instead they will have hourly pay so public and overly pay and then number of hours worked so let's call this maybe total hours okay and to get the monthly salary of a contract employee how would we get that multiply the hourly pay by total hours that's going to give us the monthly salary so this dot total hours into this dot hourly pay so that should give us the monthly salary of a contract employee now if we have to use these classes within program dot CS file we have the main method so we would create an instance of our full-time employee class so full-time employee FTE equals in two new full-time employee and then obviously there are several properties OID equals let's say one zero one first-name equals let's say Mark and lastname equals mark May and annual salary may be 60,000 okay now we have full-time employee and if we want to print their details console dot readline full-time employee dot get full name should give us the full name and similarly to get their annual salary will simply say get I mean monthly salary get monthly salary okay and let's plant a dotted line okay so in a similar fashion let's go ahead and create an instance of our part-time employee I mean contract employee as well so contract employee let's call it CDE equals new contract employee contract employee has got an ID let's say a di D is 1 0 2 first name is Sarah and let's say Sarah s is the LA S is the last name and contract and what you will not have annual salary instead there will be overly pay let's say however Li pay is 200 and total hours he worked in a month is maybe 40 and let's plant the full name of the contract employee and the total I mean monthly salary of the contract employee so let's go ahead and run this now and see what is the output we get look at that marked May is the full name of the employee and his monthly salary is 5,000 because his annual salary is 60,000 so when you divide that by 12 we get 5,000 and Sarah s is the full name and his how monthly salary is 8,000 all right so this is working the program is working as expected but what is the problem with this design so if you look at these two classes you know these two classes are definitely related you know both of them are of type employee and if you look at these two classes there's a lot of common code between them for example ID first name last name properties are same in both the classes and similarly get full name whether you know the employee is a full-time employee or contract employee you would compute the full name in the same way but concatenated their first name and last name look at that the implementation is exactly the same the only thing that's different between these two classes is full-time employee has got annual salary property contract employee has got these two properties in addition hourly pay and total hours work and then the implementation of get monthly salary is slightly different between these two classes otherwise they are almost identical okay so since we have a lot of duplicated code here the code maintainability will be a problem because tomorrow if we want to introduce middle name property we have to do it for both the classes because irrespective of whether you are a full-time employee or a contract employee you may have a middle name and in order to compute the full name we may have to again introduce that middle name within you know the get full name method of both the classes now here we are only have two related classes but in reality depending on the types of objects that you are creating you may have several different related classes you know maybe with this example we may have something called part-time employee or director employee you know things like that so if we have more related classes then you know the number of places that we have to make changes is going to grow as well and that is error-prone and number too time-consuming so code maintainability is going to be a big issue if we duplicate code like this so how are we going to solve this problem so here these two classes are related there's common functionality between them and that common functionality is duplicated between them okay so how are you going to get rid of that duplicated problem okay divide these issues I mean this duplicated code issue what we can do is we can move that common functionality into a base class okay and then we can make these two classes inherit from that base class but if we have to design a base class for these two classes then the obvious next question is going to be should we design that base class as an abstract class or as a concrete class that is a non abstract class first let's design it as a concrete class and see what are the problems that we are going to face and then we'll try and use an abstract class okay so what I'm going to do now is add another class file and call this maybe base employee dot CS and let's first make this class public and we are going to move the common code that is look at this if you look at full-time employee we've got ID first name last name you know that's the common piece of code so I'm going to take those three properties and move them into base employee class and I'm also going to take get full name method and move that into pace employee class okay so now if you look at this full-time employee class it's very simple it only has you know the code that is relevant to full-time employee or that is specific to full-time employee okay in it then what I'm going to do is make this full-time employee class inherit from base employee ok so that way this class is going to get ID first name last name properties and get full name method as well in a similar fashion I'm going to make this contract employee class and head it from base employee class and since in the base employee we already have ID first name last name I'm going to get rid of those properties and we need to retain these properties because they are specific to contract employee class and we can get rid of this get full name method because that's already available in the base class but let's leave this get monthly salary because you know we implement that method differently in these two classes ok so now if you look at this class ok the base let's get full name let's also introduce here get monthly salary method so public and get monthly salary now let's mark it as virtual method okay because the base employee class doesn't know how to provide the implementation for contract employee and full-time employee it's the responsibility of the derived classes to do that so that's the reason why we are marking it as virtual and we are going to throw and not implement it exception so okay so very straightforward base class here and put in the derived classes what we are going to do is override get monthly salary and provide the implementation specific to that class so similarly let's overwrite that here all right so let's build a solution and look at that our program still compiles and when I run this it's going to work in the same way but what did we achieve here we have moved all the common code into the base class so tomorrow if we have to introduce full-time I mean middle name property we can do that only within the base class and it will be available to all the derived classes so code maintainability is going to be much easier okay but then by creating a non I mean by creating an on abstract class that is a concrete class you know we have introduced another problem here because look at this in our organization we only have two types of employees contract employee and full-time employee so in the main method if you look at the code we are creating an instance of full-time employee and contract employee and we are using it the baby one two okay but then here since base employee is created as a concrete class there is nothing stopping us from creating an instance of pays employee class because that I can create you know base employee e equals new base employee and then I can initialize you know the properties 1:03 let's say first name equals regime lastname equals Tec okay so here basically we have created an instance of base employee class and then we can still use console dot readline base employee dot get full name and similarly base employee dot get monthly salary okay now what do you think is the problem going to be with this code when we compile this look at this when I build the solution on the status bar build succeeded we didn't get any compiler error but then we actually learn this we are going to get an exception why that's because if you look at get monthly salary in the base employee class you know this class doesn't know how to implement that so that's why it's throwing not implemented exception so when we run this program we are going to get that exception at runtime okay that's one problem another problem is our organization only has two types of employees full-time employee contract employee there's nothing called base employee it's only an abstract concept since we want our common functionality to be present in the base class we move that common functionality to the base employee class but we don't have anything like pays employee within an organization so we don't want developers inadvertently creating an instance of base employee class and using it like this so we want them we want to prevent them from doing that since we have designed that class has a non abstract class that is a concrete class you know we are not able to stop the developers from doing this okay so that is the problem with using a non abstract base classes here okay so now to prevent this runtime problem you know we can definitely achieve that by simply removing this virtual method from the base employee class and then within respective classes I can get rid of this override keyword now since we do not have get monthly salary within the base employee you know we cannot invoke that method so I can delete that line so if I run this the program is going to work look at that resume Tech is the full name that's fine but we still have that other problem we don't want developers to be creating instances of base employee class we don't have such employees within the organization and we don't want to be doing that so to prevent developers from creating instances of this base employee class what we need to do simply mark this class as abstract so since we have marked this as an abstract now look at that we already have a compiler error and if I hover my mouse over that let's quickly look at that cannot create an instance of the abstract class base employee okay so that is the advantage of using abstract classes okay so it's preventing us from accidentally creating instances of base employee class I can't even compile now so we get a compiler data which is a very good thing okay so let's get rid of this code now let's comment that now another modification that I'm going to do look at this if I run this it's going to work in the same way so we have full-time employee there and contract employee there okay now another modification that I'm going to make to this base abstract classes I'm going to introduce this get monthly salary method within the base employee class so let's actually copy the full signature and I'm going to make this abstract method so if we make the method abstract we don't have to provide the implementation okay only the declaration and within the derived classes we are going to provide the implementation by overriding it okay so let's go ahead and run this and see if it works in the same way so it's going to work in the same way but why are we introducing this abstract method went into the space employee that's because the advantage we get by introducing this method as an abstract method is that all the derived classes now are forced to provide the implementation forget monthly salary method okay because this contract employee class is in a heading from base employee and base employee has got this abstract method so contract employee has to provide implementation for that method otherwise look at what's going to happen we will get a compilation error again that's a very good thing because if you have different types of employees contract employee full-time employee may be another type of employee called part-time employee any type of employee should have a salary okay so that's why you know when you include you know this abstract method get monthly salary in the base class it's going to force all the derived types to provide implementation that way we are not going to provide I mean some functionality to you know employee related classes all right so in short we would create an abstract class when we want to move the common functionality of two or more related classes into a base class and when we do not want that base class to be instantiated so in this case what did we do we move the common functionality of full-time employee and contract employee into the common base class you know base employee and we made that abstract because we don't want developers to accidentally be creating instances of that base employee abstract class now you know if your requirement is such that if you want you know the capability of instantiating the base class then you would do normal inheritance there and you would not mark the base class as an abstract class okay but then in short this is the answer we would create an abstract class when we want to move the common functionality of two or more related classes into a base class and when we do not want that base class to be instantiated so here is our base abstract class and the two derived classes that's it to today thank you for listening have a great day
Info
Channel: kudvenkat
Views: 830,289
Rating: 4.866468 out of 5
Keywords: Why, where, when, abstract class, example, realtime, real-time, real world
Id: yyU3bXyc_oU
Channel Id: undefined
Length: 20min 28sec (1228 seconds)
Published: Fri Sep 27 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.