#6.2 Java Tutorial | Super Method

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you [Music] so now we know about inheritance we know about constructors in this video we'll talk about super keyword our super method so what exactly super is but just to understand that what we'll do is let's try to understand the the working of constructors when it comes to in high trends and then we'll understand the real power of super keyword for that what I will do is I would simply use a simple class name as a which I which I'm doing from a long time so let's say we have class a and we have class B and both these classes they have a constructor so let's say in this class a I have a constructor which is public a and I'm printing there so let's say this is in a okay we'll simply say in a and in Class B again I have a constructor here which is public B and in this I'm printing the same stuff which is in B so we got Class A and Class B till now we don't have any relationship between B and a when our do is I will simply say a Class B extends Class A so Class B is a subclass now and a the superclass now if I create the object of a so if I say a obj one equal to nu a so now I can see I'm creating object of a not of B and if I simply say I mean if I simply create the object of it of course when you play it the object it will cook it will call the constructor white which will call the constant of a and if you run this code okay so we got our output there which is in a write that means when you create an object of a you get the construct it it calls the color of a but how about if I create the object of B now base B different right because B extends a so B the subclass now so let's see how it how it behaves when I create object of B now when I create object of a it calls the constructor of a but what about B if I create only top B what happens now will it call cost of B let's try so if we run this code you can see that's weird right we are creating object of B but why we are getting observing concept of a so that means when you call when you create object of subclass it is calling this subclass constructor plus it is also calling the superclass constructor so the concept says whenever you try to create it the object of subclass it will also create the it will also call the constructor of superclass so it will call the conceit of superclass there it will call the cursor of subclass ok that's why you got in a and in B so this is the point to remember whenever if anybody could adopt of subclass it creates I mean it calls the cost of super class and subclass both now what I will do is in this two class I will have two more constructors and which will be a parameterised constructor as we have seen that we can create parameters constant right we can have to construct in the same class that's considered overloading right so we have this constructor here let me copy that and we'll do the same thing here we'll say this is in a int so I'm creating a constructor which is in a int let me copy that and we'll paste it here also and we'll drink in v8 so you can see in Class A and in Class B we have a constructor okay so you can see we have we have two classes a and B and both has two constructors our default one and a parameter wise knife if I create the object of B which is default one if I than this code of course it will call the default gossiped right let me pass a value here if I pass a value five so if I pass the value 5 it will call the constructor which is the parameter instead of B right but what about the constructor of a will it call the default one or will it call the parameter wise one as we are passing a parameter we expecting that it should call both parameter right a and B both parameter and if run this code it's been different we got the output as in a and in b int it's because whenever you create object of subclass by specifying the parameter it will call this specified constructor of subclass but it will call the default character of superclass so it doesn't matter what type of object you create you're creating an object with parameter wise one or default one it will call the default constructor of superclass that conducts there is something it will do what if I don't want to call the whenever twice I don't want to call the default one I want to call the parameter wise one how to do that so what we'll do now is first of all let's try to understand why it is calling a default constructor so we get that widest point default master then to understand how to call the parameter wise one so what happens is every constructor so every constructor in sub class will have a by default method called as super so even if you don't mention that even if you don't mention super it will be calling I mean it will be super there so if you don't mention super then it will be there by default it will be given by your compiler okay and we have a super here also now since we are creating a constructor which is bypassing five which will call the parameter wise consists of B and it is calling super when you say super super simply means it will call the constructor of the superclass so when you say super and bracket it will call the constructor of superclass now since we don't have any parameter here because by default if you don't mention that your compiler will put the default one which means it doesn't have any parameter and that's why it is calling the default one but how about if I pass this I here if I'm passing that I there what happens is this will call the parametrized constructor of the superclass which is the public a end and that's why I put on this code now so we got in a int in B in so when you create a constructor by passing a parameter it will call this constructor first but it will not execute this this statement here before executing a statement it will call the constructor of superclass by passing a value okay which is I I mean so you are passing this value and it is it is spending that statement there okay and that's why that's how your super works but how about if I don't let's say if I don't pass a parameter it will call the default constructor of B right which is of course when you don't mention the parameter here it will call the deform instead of B and it is calling the Falken sitter of a which is this but it's say I want to call the default of B and parameterize of a what you can do is I can simply pass a number here so if you pass five it will call the parameter wise one that's why the output you will be getting is in a int and in being so if you learn this code that is what we are getting here okay simple so that so you can specify a super matter so this is super material
Info
Channel: Telusko
Views: 117,865
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java
Id: IPJYL18NCpw
Channel Id: undefined
Length: 6min 47sec (407 seconds)
Published: Fri Feb 02 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.