#6.1 Java Tutorial | Inheritance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] so once we got the idea about the basics of java we have talked about arrays and objects static keyword and all the stuff now let's move towards the oops concept so when you say oops it provides you lots of features so I need to write you inheritance abstraction data hiding and capsulation so we start with the first one which is in height in seer so we talk about inheritance what exactly it means is again we can go for some complex example but I believe in making thing or keep it simple right what we'll do is we take a simple example again which is very typical example when talking about inheritance we'll go for the same example here and that is to create a calculator so what we'll do is we create a class first calculator okay so that's our class here and in this class I want to perform some operations and the only operation I want to perform here is let's addition of two numbers so what I will do I will say public which returns int the method name is add which takes parameter which is I and the second parameter which is J it returns I plus J quite simple right so we got this calculator class here and now let's create the object of it now how do we get object of calculator we simply say calculator we'll say this is c1 equal to new calculator now what I will do is I will simply create a variable here I will say this is Inc result equal to c1 dot add and if I pass two values if I pass four five and four and if I print the output here it will print result so if I on this code we are getting the output which we are expecting here and that's our output which is nine so this is one class white so let's imagine in future in future if let's say if I am creating this object of calculator and and then I'm giving this calculator object to my customer as I develop I am giving this object to a customer customer is very happy because this character can perform one operation and this code let's say this code code belongs to some other class some other file now in that scenario if this class belongs some other file and let's say as a developer you have deleted this file I only have the class file I mean okay let let me just let me just make it simple let's say if a user comes to me and okay I want one more feature now I want this calculator to select also not just add it it also wants to subtract it I mean subtract two values but the problem is due to some problem you cannot make some changes here because we don't have the source code of calculator imagine imagine you don't have the source code for calculator now what you will do but you have the class file what is class file so when you combine your Java code you get the class files right so let's imagine you don't have this source code that means you don't have the Java file but you have a class file so what you can do is you can create one more class in that scenario will name this class as calculator advanced so this is advanced Calcutta which should have two features again since you are creating a new class we need to define that ad with a once again right so we'll say public in fact when you want to define it let's just copy paste copy and paste I mean in real life we will you will type it because it don't have the source code and again you will paste it but this time this should be sub and this should be subtraction all right so now we got a new class which has two features one is addition and subtraction so from your main method in shop hitting object of calculator will create object of calc advance so here we'll say result one is the addition and we say end result to is it will be subtraction now so since we have the new class we can perform the subtraction operation also and let's print a result one and let's print result two now this will be resolved too so now if we run this code it will work right because we have a new class all together which has two methods but hold on you already have the class file off of calculator right can I use this ad in this class why to write the same code two times this is redundancy right we are ripping the codes so will not repeat the code will simply fetch that feature will simply fetch this feature of add in this class now how do we fetch a feature by using the concept of inheritance so we'll simply say extends calculator now in this scenario this class becomes a super class and this class which is calc advanced is called as surplus so superclass will have only one method called as ad but subclass will have to methods one from this class itself which is advanced calc and one method will be coming from calculator so this is super and this is sub so we got a superclass and we got a surplus and now even if you run this code now you can see it still works we are getting this subtract method from advanced calc itself but we are getting this add method from calculator okay it is that simple that's how you use inheritance this is basically a single level in items because we have only one levels right so this is your super and this is your sub we can use some more terms in shops simply saying super sub we can also say this is a peasant class and then and that scenario this becomes a child class we can also say this is as a base class so if this is a base class now this will be called as or did I plus so in Java we use a term colors event super and sub in in the concept of oops we use parent and child in the concept of seep of C++ we use base and derive so in Java will be using super and sir will not use this two keywords but you can say that I mean no one will kill you if you mention parent and child it's just that in Java we use super and sub so that's how you create a single level inheritance now we can also achieve a multi-level inheritance here now how to do that let's let me do it one more calculator here we'll say this is calc very advanced calculator okay which will have which will have three features the addition the subtraction and one more thing which is multiplication so ensure of creating three methods here I will simply create one methodology which is multi which takes two parameters int I comma int J and it returns I into J okay and I want this class to extends calculator I mean the Advanced calculator that's it because if this class extends Advanced calculator advanced capita extends calculator so this class will have all the features of advanced I mean as and sub so if you imagine this is our grandfather this is a father and this is a child okay and now I'll show creating object of cows we can create object of advanced variation calc very advanced and we can call three methods here so the pay streak this is result three and this will be multi let us say this is 2 & 3 and let's also paint the third result so we have achieved a mighty level in height in Co so we have this first level so if you talk about these two classes this is first level in it is a single level in heightens if you talk about all these classes this is again this is multi level but if we talk about only this two this is again single okay because we are talking about this - only that is single this - single and all this three there are multi level so in Java we can achieve single level inheritance we can achieve multi-level inheritance but how about multiple because if we talk about oops concept it also provides you multiple inheritance but unfortunately Java doesn't support multiple inheritance why exactly doesn't support multiple inheritance that we'll see in the next video but time we remember we we we work with inheritance like this again in heightens has two things one is there are two relationship in itens just to let you know this is called as is a and has a now what it means whenever you have a class which extends another class it is called as easy and whenever you have a class which in which you are creating the object of another class it is called as has a example in heightens demo can access the features of add because we are creating the object of calc very advanced I'm saying that in this class I have the object so when I say I has I have so that is has a relationship and when you extend that is easy example when you talk about dog is dog is an animal so dog extends animal but when you talk about a bathroom so bathroom has it has a tub right so that's a two difference I need in a chance we have okay but time remember only easy so yeah that's it that's about inheritance single level and multi level or in the next video we'll see why Java doesn't support multiple inheritance thank you so much
Info
Channel: Telusko
Views: 182,780
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java
Id: _9R_in_7cT4
Channel Id: undefined
Length: 8min 46sec (526 seconds)
Published: Wed Jan 31 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.