[Music] welcome to narish Technologies and in this session I want to demonstrate you about inheritance in objectoriented programming so let us understand what is this inheritance inheritance it's a mechanism of consuming the members that are defined in one class from another class see we are aware that class is a collection of members and the members that are defined in one class can be consumed from another class by establishing a parent child relationship between the classes say it's a mechanism of consuming the members of one class in another class by establishing parent child relationship between the classes see generally all of us know one thing what is it all our parents property belongs to us so children get the right on the parents property so what is the uh why because that is law that is law according to law all the property of the parents belongs to their children exactly the same principle can be applied here also what is it suppose I have a class with a set of members in it and I want the same members in another class class two so if I want it what I can do is I can copy paste all the code from class one into class two copy the code in class one paste it in class two but understand if you copy and paste the code like this that is called as rewriting the code we call that as rewriting the code so rewriting the code will have a impact on the size of your application now application size will grow if application size grows ultimately there will be effect on the performance of the applications so if you want to overcome that particular rewriting process and reuse the code the best option that is available for us is inheritance simply what to do is establish a relation between the two classes what relation parential relation and once you establish a parential relation watch it all the members of the class one can now be consumed under class two class two can consume the members of class one now so that will that can happen for you if at all relations are established between the classes so what I said you it's a mechanism of consuming the members of one class in another class by establishing parential relation between the classes which provides reusability so basically the advantage with inheritance is it provides you reusability it provides you reusability why because I'm just trying to consume the members defined in one class from another class not only another class here you can consume from any number of classes like this means a class can have n number of children and all the children can consume the members of its parent class so that is the big idea between sorry there's a big idea about the inheritance so it will provide you reusability okay fine now how to um perform this inheritance if you want to perform this inheritance okay you need to establish relation how how exactly is it possible listen I have a class in a and in this class I'm defining a set of members set of members now I'm going to have another class here Class B and this class B I want to inherit from class a colon a a very simple process how you can perform this and now you can start consuming the members of a from here you can start consuming the members of a from here so that's a basic process how you can establish relation between the classes so if you just check out the syntax now simple modifiers this talks about your public private and all these things class that is a keyword to tell you are defining a class here child class colon parent class actually the terminology parent class child class you can also call them as derived class and Base Class means parent class or the first class what we are working can be called as a means here a this class you can call it as a parent you can call it as base okay or you can call it as super also any any terminology you can just use different people just call this in different way so you can call with any name here and what about class B Class B can be called as a child or derived class or subass whatever it is all are same I choose the terminology as parent and child but even I call it as a base class or a call as a super class all are same and a child a call it a derived class or call a sub class all are one and the same but I will prefer to use the terminology as parent and child okay fine so this is the syntax how you can just use inheritance in your code and is an example how you're going to perform now let us practically perform this inheritance in your application and one important Point here what do you know in inheritance in inheritance in inheritance child class can consume members of its parent class as if it is the owner of those members the big important thing to understand here is child class can consume the members of its parent class as if it is the owner of that particular members except private members of parent so what do this except this private members of the parent see children generally children have right on their parents property as a children tomorrow I can take over my father's business I can take over my father's property cars buildings whatever it is but I cannot take over my father's job the reason is the job what my father is doing may be based on his qualification and his experience and tomorrow I cannot take over his particular job now so you understand that the job is completely private to my father and that is not inherited to me but reminding everything business money properties whatever whatever I can take all these things I can take over all these things except the private members so exactly now also the same principle what is it see why I just used the word parent class can consume the members as if it the Warner is I can drive my father's car as if I'm owner of the car no one can stop me okay so that's why I'm telling you here what is it child class can consume the members of the parent class as if it is the owner of those particular members let us see practically I'm opening a new project in Vis studio console application and let me call it as inheritance project okay by default I have a classia called program and uh I want to rename this class as class one Open Your solution Explorer and rename this class just let me call it as class one no issues when I rename It'll ask you about renaming this name throughout the project yes you can see now the class name is also renamed I'm deleting the main method I don't want it and let me Define some few methods in the class public void test one console dot right line method one next public void test to console. right line method fine so like this I'm just going to Define two methods in the class now I want the same two methods in another class that is class two let me add it first there's my class two I want the same two methods what are present in class one and class two so one thing I can do is I can copy the code in class one and I can come to class two and paste it here but that's not going to be reusability that is rewriting the code which affects the size of your application so without doing that what you do now you know let us perform inheritance here colon class one inherit once you perform this inheritance class two contains the two me two methods in it why all the property of the parents belongs to the child and now you see let's write a main method here and next create the instance of Cl class two not class one class two c is equals to new class two creating the instance and now C dot when you say C dot in intellisense you can see the two methods test one test two so that's what I said you child class can consume the members as if it is the vulner you were able directly by using the child class instance that's what the point here C dot see test one now if you see this test one if you see the description here what is it showing you void class one. test void class 1.
test what is that yes the method test one belong to class one only now but what we're discussing here Class 2 can consume like a Warner yes no doubt about it see I can drive my father's car as if I a Warner but still the registration name will be my father exactly class two can call the method just like the method is of its own but internally the method belongs to class one so Class C do test one C do Test 2 and console dot R line and let's execute the code yeah if you watch your output now you can just understand it was calling you the two methods method one method two got executed so right now we have currently created instance of class two and we calling the methods of class one what are the two methods test one and test two let us add a new method into class two so a new method public void test three a new method was added now console do right line simple method tree and now coming below I can call this method C dot there are three methods right now two methods inherited from your parent class and the third method is defined under the current class you can watch C DOT test three but test one belongs to class one test two belongs to class one test three belongs to class two class two the method is of class two so calling you all the three methods now let's run the code you can watch all the three methods got executed So currently if you raise a question like how many methods are there in class two the answer is three class has to has three methods why see all the property what my father has given me plus all the property what I'm earning is whole my property only so what is my property means I don't say what I earned I also say what my father has given me so exactly how many methods are there in class to me I say three methods two inherited from parent and three is the new method what you defined on your own so total you have three methods under class two but what about class one class one contains two methods there are only two methods in class one but class two contains three methods in this case so this is the process of inheritance okay a very simple process simply put a column and write the class name but when you're working with inheritance there are some few important things what you required to learn or what you required to remember what are those let me tell you one by one we'll discuss I have six important things to learn here let me teach you one by one the first important thing what do you know the first important thing is parent classes Constructor must be accessible to child class otherwise inheritance will not be possible otherwise inheritance will not be possible what are the meaning of the statement what are the meaning of the statement is right now class one class two both these two classes contains implicit Constructor what is that yes every class contains an implicit Constructor if not defined explicitly okay so we already learned about it in our Constructor session so in the Constructor session I explained you every class contains a con Constructor that is implicitly defined if not defined explicitly so right now class one class two both these classes have a Constructor but understand the point here if a Constructor is implicitly defined that is a public Constructor and that Constructor is now accessible to class two class two can access class one's Constructor now but if at all an explicit Constructor is defined in the class now suppose class one I'm defining a explicit Constructor if I Define this construct as public class one and writing some code in it console do right line class one Constructor is called class one Constructor is called so right now come back to class two and execute your class two when you execute your class two see what is happening first class one Constructor is called why because whenever the child class instance is created the child class Constructor will implicitly call its parent classes Constructor that's a rule the child class Constructor will implicitly call its parent classes Constructor so right now child class also contains a Constructor what Constructor implicit Constructor and that is now calling the parent classes Constructor now but parent class Constructor is not implicit it is explicit now and under that I've written some code because I written some code that is getting printed even I don't Define this Constructor also there will be a Constructor there will be a Constructor but be careful when you're defining the Constructor if you don't use if at all you just going to use private here or don't use any modifier here don't forget default scope for the members of a class in C is private because the default scope is private this Constructor is not accessible to class two and if you try to run class two you will get an error you will get an error what is that error read the arrow message if you read the arrow message what's it telling you class one. class one what is class 1. class one class one is the class name class one is the Constructor name this is class name this is the Constructor is inaccessible due to its protection level and the error occurred in class 2.
CS the error is in class 2. CS why because whenever you create the instance of a child class the child class Constructor will implicitly call its parent classes Constructor right now class two implicitly class two Constructor implicitly calls class one Constructor but this is not accessible why it is private already we have learned it what is this in inheritance S Class can consume members of its parent class except private members apparent so you cannot call the private member now what is the private member The Constructor Constructor cannot be called and if the Constructor cannot be called inheritance is not possible that's what I'm telling you parent class Constructor must be accessible to child class otherwise inheritance will not be possible okay I'll just show you one thing watch let us Define a Constructor in class two also class two so here also I defined the explicit Constructor that's it if I don't Define also it is present but now explicitly defined run your code and the problem I already demonstrated it must be public so let us make it public public class one and now tell me is this Constructor accessible to the child class now yes it is now if you run this first what is called class one Constructor next class two Constructor so execution always starts with parent class Constructor why when you create the instance of the child class the child class Constructor implicitly calls the parent class Constructor suppose class one has any parent class class one Constructor will call its parent class Constructor if that has a parent class it it calls its parent class Constructor suppose if you have 10 classes now and you create the instance of the 10th class what is going to happen you know it keeps on calling each and every Constructor 10 will call nine nine will call eight eight will call S 7 to six like this until one and execution starts from class one Constructor and executes all the 10 Constructors I'll tell you the reason why to call the Constructor we'll talk on it but that is the process here you might doubt class two Constructor is not public class two Constructor doesn't require to be public why class one Constructor should be accessible to class two not class two to class one when class one Constructor should be public you know if class two has a child class then class two Constructor must be public if Class 2 doesn't have a child class there is no point of declaring it public or if it don't declare public also no matter so that is the point if you want we can declare it public but that's not a matter at all now okay now again you can watch your output first what is called class one Constructor I'll practically prove you how the execution takes place actually we have two classes in the project but main method is in class two so execution starts with class two no doubt so now execution starts with class 2's main method now so let us do one thing let us add a breako here let's add a break point on on this line okay so how can you create a breako just select you go to debug and you can just use toggle breakpoint so once it's a toggle breakpoint a breakpoint gets created and now you can see a red color line here a red color identification background color changed now run your code so when you're running your code when the execution reaches the line when the execution reaches that line execution stops there actually it won't go further if you want to see the output now if you want to see how the execution takes place you can watch it by pressing F10 by pressing the F10 we can see the output okay just watch F10 when I press F10 it move to next line next line next line next line like this line by line execution you can check it out right now so F10 will take you to the next line and you can watch your output so if you just watch one more time let me start okay execution stopped on that line first F10 so that line of code executed no watch your output that line of code is calling you Constructor one Constructor two next again F10 so test one also executed method one again F10 test two executed method two again F10 test three also executed method three so like this this we can see the line by line execution okay well watch again start if I say F10 it'll go to next line actually we call it as a step over step over we have another statement called F1 that is a step into what is that step into it will step into the Constructor now why whenever we create the instance of a class implicitly The Constructor of the class gets called so right now if I use F1 here it will jump to the Constructor watch it from here control jump to the Constructor and it won't execute the code in the Constructor watch it from here it will jump to class one that's what I'm showing you what is it whenever you create the instance of the child class child class Constructor will implicitly call its parent class Constructor you can watch out now child class Constructor is calling its parent class Constructor now and once it is calling the parents class Constructor the parent class Constructor executes okay now you can use F10 come to next line okay so just use F10 to come to next line now see first which Constructor is called first class one Constructor check your output back to class two Constructor and executing you class two Constructor check your output back to the main method from where the execution started back to the main method where the execution started so why I showed you is this to prove you child class Constructor implicitly calls the parent class Constructor is it proven that so parent class Constructor should always be accessible to child class if not accessible inheritance will not be possible but the thing to understand is why it should be accessible why it should be accessible the reason behind it is if you call the parent class Constructor then only parent class variables will be initialized and they can be consumed under the child class if the parent class variables are not initialized you cannot consume them under the child class if you want to consume them under the child class must and should that should be initialized if not initialized you cannot consume them see child class is dependent on parent class so parent class must be initialized first then only consumption is possible in the child class that is a reason why it will call parent class Construction initialize all the members there and then you can start consuming them under the child class so this is the first rule of inheritance what is it child parent class Constructor should always be accessible to child class if not accessible inheritance will not be possible is the first important Point second point in inheritance child class can access parent classes members but parent classes can never access any member that is purely defined under the child class see one thing according to law children have right on the parent property but parents never have right on the children's property it is only the children's responsibility to take care of the parents okay but legally parents doesn't have right on the children's property exactly parent class can never access child class members that are purely defined in the child class if anything is purely defined in the child class parent class cannot access it watch it right now come to class two and in class two main method I'm going to create the instance of class one class one that is parent class instance p is equals to new class one class 1 p is equals to new class one and once after creating this what I'm going to do now is I'm going to call the members now P Dot and call the methods you can watch the list it is showing you test one and test two it is not showing you test three why is it not showing you because that is not accessible to you and remember if intelligence is not showing you you cannot perform this if you try to blindly do it you'll get off an error intelligence should show you all the things now so just test one I can call test two but if I try to call p.