Part 21 - C# Tutorial - Inheritance in c#.avi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to prism technologies I am benkard this is part 21 inheritance in c-sharp in this session we will learn why inheritance advantages of inheritance inheritance syntax and a few concepts related to inheritance so let's first look at an example of ye be actually required inheritance now if you look at these two classes on the slide we have full-time employee and part-time employee two types of employees working within our organization now whether if he is a full-time or a part-time employee there are certain things that are common to them like first name last name email and you know calculating and printing the full name of that employee you know most of this code is actually common between these two classes if you look at these two classes there is a slight difference you know for a full-time employee we we have yearly salary whereas for a part-time employee we have hourly rate so now that's the only difference basic difference between these two classes in reality there might be several differences but just to illustrate you know why we might have to use inheritance you know let's take this example so the difference between these two classes is that you know this employee has a yearly salary field whereas part-time employee has an hourly rate feel okay so these two classes instead of duplicating a lot of these discs oon between these two classes what we can actually do is we can abstract away most of the common code into the base class as you see here now what is the advantage of doing this number one you get code reuse and when you get code review so it could reuse the amount of code the testing team has to test is drastically reduced and the amount of code that is there in in the application is also reduced because of which you know the probability of errors within that code is also reduced so it could save a lot of time as well as you know it could also prevent from unwanted errors within the application so inheritance promotes reusability and with that we get you know savings of time and you know the probability of less number of errors there it also uses errors okay so if you look at this we actually have rewritten this class all the common functionalities in the base class right now and you know basically whatever are specific to full-time employee or part-time employee they are only present in that respective classes so in our example only year's salary was specific to time employee and hourly rate was specific to part-time employee so these class specific code is present in those respective classes whereas the common code is abstracted away into the base class so that it can be reused across these two derived classes now let's go ahead and look at an example of how to do this okay so if I have to create an employee you know class public class you know this is going to be a base class public class employee and in this base class we are going to have all of our common properties like first name last name email and then full-name so public string first name and similarly they'll have his last name and maybe email actually let's get rid of the public access modifier for fields it's not a good habit to have public access modifier when we talk about properties will come to know why so string email so these are common to both full-time employee and part-time employee so print full name so basically all this piece of code is common to both types of employees now let's say I'm going to create our full-time employee so full-time employee that this person is going to inherit from the employee class now this is a syntax for inheritance your base class I mean you derived class : your base class so now what happens whatever is present in this employee class will also be available to your derived class okay so basically I mean these fields are private now at the moment but since if they are public they will be available to your class we look at those a little while later you know we create fields and then surrounding them we actually create properties so that they will be available to your derived class so similarly here what is specific to a full-time employee maybe a nearly celery soup float eerily celery and similarly I can go ahead and create a part-time employee class so part-time employee class inheriting from the employee this piece of code again will be available to this class and then this part-time employee may have something like hourly rate hourly rate for now let's make this public so these are available too but keep in mind making your fields public is not at all suggested in real time instead we make our fields private and expose those private fields to the external world using properties which we'll be looking at in a later session so similarly let's make this public and thus public we will be talking about access modifiers as well in a later session here the idea is to learn about inheritance let's concentrate on that concept okay so now we have created full-time employee part-time employee now I can go ahead and create my full-time employee so full-time employee FTE is equal to new full-time employee I look at this full-time employee object you know it doesn't have the print full name prop you know method or it doesn't have first-name lastname you know properties but still I will be able to use them so for example FTE dot you know you can basically say first name is equal to maybe regime and similarly we can say ft dot last name is equal to tech so I'm able to initialize the table jecht with these two properties you know in spite of that code for a full-time employee non presenting not present here it since this class is inheriting from the employee class all these will be available to your full-time employee so similarly I can go ahead and call the print full name method as well and even that property is not present in this class I mean that method is not present in this class and the employee are full-time employees specific feel that which is yearly salary so yearly salary is equal to whatever may be fifty thousand five hundred thousand whatever okay now look at this on the other hand you will not have access to hourly rate because that field is specific to part-time employee and if you look at here we are creating an object of full-time employee so your full-time object full-time employee object will have access to all the cool that's present in the base class plus whatever code specific here so in a way we can say this is this full-time employee class is actually a specialization of your employee class in the sense it has all the capabilities of your base class class additional capability that is present in that class so your derived class is actually a specialization of your base class I mean we can compare this to like you know a general surgeon versus a specialized doctor for example if you take you know maybe a gynecologist she'll be able to perform the duties of the generals general surgeon as well as she specialized in gynecology so at times she can even be you know behave as a general general surgeon okay so that's kind of basically the difference between a parent and a derived class so now we don't have any implementation here but if you want some implementation you know we have seen in the other examples if we want to print the full name we can make use of the consume class to write line and we can say first-name plus and then give it space and maybe last name okay so now if we go ahead and drop this you will see obviously a full-time employee object is created and its first and last name is printed there similarly I mean if you want to create a part-time employee you can exactly do the same thing and whatever is present in your base class is actually available to your part-time employee as well so part-time employee dot first-name so that's still available so maybe we will call this part and similarly last name is equal to maybe time and then if you look at this this object will have access to hourly rate from the intelligence you can see that it has access to hourly rate but not yearly salad because yearly salary is specific to full-time employee and similarly we can also call the print full name in this case part-time will be printed so presumed Ekstrom the full-time employee object part-time from the part-time annually object so basically inheritance allows could you could reuse so why inheritance you know in object-oriented programming we have four pillars inheritance encapsulation abstraction polymorphism and inheritance one of the primary pillars of object-oriented programming okay several other concepts are actually based on inheritance if you have to understand polymorphism you need to understand inheritance properly so so that's the primary pillar of the object-oriented programming it allows code reuse we have just seen an example and when we reuse code it can reduce time errors and we have also seen that we will specify all the common fields properties methods in the base class which allows reusability in the derived class we will only have fields properties and methods that are specific to those respective derived classes okay we have looked at an example of inheritance syntax as well you know you will have a parent class and the implementation of that as you can see in the slide and the derived class you you include a coolin and then specify the parent class we have seen that within our example so here we have our derived class for example part-time employee : employee the parent class ok in this exam derived class inherits from parent class c-sharp supports only single class inheritance that's very important to understand ok c-sharp does not support multiple class inheritance in the sense for your given class you can only have one base class at a given point of time I cannot specify another base class let's say for example this part-time employee class is inheriting from base employee class let's say I have another class maybe public class a this is another class now can I make my part-time employee class inherit from employee class as well as you know class a at the same time this is not practical if we try to build this build solution we will get an error cannot help multiple base classes no part-time employee cannot have multiple to remember a class can have only one base class at a given time but however multi-level class inheritance is possible for example part-time employees inheriting from employee class so you can say plus a inherit from part-time employee class now this class a will have all the properties all the code that's available in part-time employee plus the code that's available in employee so if somebody now goes ahead and create an an a object so let's say a a one is equal to new a now what happens a one will have access to everything that's available in part-time employee as well as employee so in in part-time employee we have hourly rate in employee we have the rest of the properties as you can see them so multiple class inheritance at the same time it's not possible but multi-level class inheritance is possible class a and editing from Class B and Class C inherit from Class A maybe so the the multi-level class inheritance is possible okay but remember c-sharp supports multiple interface inheritance at the same time you know but we haven't spoken about interfaces yet probably we'll cover that in a later session but just I want to bring that point here so that it's there in the back of your minds but we'll do an example of that and we actually talk about interfaces so remember c-sharp doesn't support multiple class inheritance but it supports multiple interface inheritance okay and another point to keep in mind is child classes a specialization of base class which we have already seen you know our part-time employee full-time employee you know it's actually a specialization or employee class in the sense it can do everything that your base class can do plus whatever is specific in that class so in a way it's a specialization of the parent class okay base classes are automatically instantiated before derived classes and this is another important concept to actually understand let's get rid of this one so anytime we create an instance of a child class okay here full-time employees a child class so anytime you create an instance with this child it lasts it automatically creates an instance of base class in the sense before the child class constructor gets executed before the child class constructor gets executed the base class constructor will be automatically executed and it makes sense because look at this this print of full name is actually an instance method now if you are new to methods and if you are new to you know understanding this difference between static and instance methods please refer to my previous video tutorials we have discussed that there okay now here print full name print full name is actually an instance method which is defined in our employee class now if you have to invoke an instance method we know that we need to have an instance of that class so obviously and this this principal name is actually present in the parent class and if you look at the main method what we are doing here is actually creating an object of type full-time employed the derived class we are not creating an instance in the parent class but printful name is present in the parent employee class so if that method has to be available for this object then that class needs to be instantiated even before we can create an object of this class okay so when you try to create an instance with your child class the parent class gets instantiated even before that so parent class constructors are called before Chael the class constructors let's actually look at a simple example of that so that it makes sense so let's get rid of all this code I mean in terms of just looking at it as a parent class in the base class so public class parent class okay and let's say public parent class and we'll say console.writeline parent class constructor called okay so if you look at this we have a parent class and it has a parent class constructor similarly we can say Oh Blake class chil the class inheriting from parent class and we can have a child class constructor so obviously public child class and probably we can say copy-paste are there and if you look at this tail the class constructor call now if you look at this cool it's pretty simple we have a parent class a constructor within that trails last a constructor within that and now if we go ahead and create an object of child class so change the class CC is equal to new child class you're only doing that so you're creating an object of the child's class but look at this we know the fact that you know before chalice objects are created children in a parent class objects that gets created so obviously parent class constructor needs to be called so when I go ahead and run that you see that parent class constructor called child class constructor called do we are not explicitly calling you know parent class we are explicitly not creating middle class object okay on the other hand let's say for example in the parent class I have another constructor and remember we can overload constructors we have seen this as well in our previous video sessions so let's overload a constructor maybe I want to pass in something like a string data type so string message okay now console dot write line all we have to do is let's print that message okay now if you look at this one the parent class has two constructors okay one which takes parameter and the one and the other one which doesn't take the parameter now when I go ahead and create this Chancellors object obviously we know that parent class constructors are called but which one of them will be called by default the parameter less one will be called which means this constructor will be called let's go ahead and run that so parent class constructor call which is the parameter less constructor but on the other hand let's say when I create a child class object okay when I create this child's last object I always want to make sure this constructor gets called so I pass in a message and that message can be used to initialize some of the fields in the parent class so how do I ensure that you know the constructor of my choice in the parent class gets called when I create a child class object so how can we do that that you can actually control using the base keywords base and then look at that your parent class so base you know base keyword refers to your parent class so we know that in the parent class there are two constructors and the intelligence clearly shows that one constructor without parameters another constructor with the message and now you can pass whatever message you want here for example let us say message from or you know derive the class control end parent class okay basically from the derived class we are controlling which constructor gets called when an instance of this parent class gets created okay so now when I go ahead and run this instead of this constructor getting call this constructor gets called and it end and you know this message gets passed into this constructor that message is printed and next the child constructor call will appear so derived class controlling parent class child class constructor call okay so it's also possible that for the derived classes to control which constructors keep called when parent class objects are created okay so here you can find additional resources for c-sharp and asp.net interview questions thank you for listening have a great day
Info
Channel: kudvenkat
Views: 519,672
Rating: undefined out of 5
Keywords: Part, 21, C#, Tutorial, inheritance, in, c#
Id: OLTk0xAVY00
Channel Id: undefined
Length: 22min 3sec (1323 seconds)
Published: Sat Jun 09 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.