Why we need constructor inside an abstract class ? || Popular Java interview question

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everyone I hope you guys are doing well so today in this tutorial I got a very interesting question for you guys so let's listen to that all right so let's say you know that your abstract class can have a constructor but we cannot create an object of an abstract class so if we cannot create an object of an abstract class then why we have a constructor or why we are allowed to write a constructor inside our abstract class what is the importance of the constructor inside an abstract class and why do we need that so it's a very important interview question some time and let's get the answer for that today hi everyone you are listening to our village from selenium express so let's get started alright so now let's understand why we need a constructor inside an abstract class alright so before I tell you that let me walk you through your problem let me make you understand that why actually we need that what's the problem right so that we need a constructor inside an abstract class alright so now let's go for that so now let's say the class that you are seeing over here in my screen lesson abstract class right so you can see that there is an abstract keyword before this particular class called a B demo right so that's then abstract class all right so now let me create few variables over here inside this class let's say int i and int J all right so I do have two variables over here inside this particular class called a B demo which is abstract right so now let's say I have another class over here called demo right and let's say this class called demo extends right this exchange my a B demo class right which is a abstract class right so I'm gonna extend this class over here alright so now let's say I have few variables over here inside this class as well let's say int K and in l right so now what I want I want to initialize this variable during the time of object creation so let's say whenever we're creating the object for this class called demo during the time of object creation only I want to initialize these variables and for that I need a constructor right so let's say I want a constructor over here so you know that constructor name and class name is always same so I'll write them over here and they recall and let me make it public so this constructor is public right and now I want to initialize this variable so how can I do that I can write it into K and into L and the moment I write this K and L over here you can see there is an ambiguity right so it is K over here so this is K and this is L over here so this is L so there is an ambiguity and to resolve this ambiguity I can use this this keyword right so I can set this start K this means this K right equal to K so this K equal to this K and again this dot L equal to L well all right so there you go my constructor is ready so now I'm only initializing these variables but I'm extending to this particular class over here right so this class has all the two variables over here let's say I and J so I want to initialize these variables as well so what I can do over here so I can I can take two more variables so I can say int I and int J as well isn't it so now I'm going to write again T's dot I equal to I and this dot J equal to J right so let me put a semicolon over here and let me save it all right and this looks good all right so now we are done with the constructor right okay so now let me create an object for this class called demo and let me use this particular constructor to initialize the variable so create an object I need a man matter so let me have that and there we go so I can write demo do you kill two new demo and over here I need to fast these variables right so I can pass a variable for I I can pass a variable called J I can pass a variable per K and I can pass a variable for L and that's that's how we actually do programming right so now the question is so let's say if I do have an another class okay and that class is also extending every demo right so let me create a man of the class over here very quickly and let's say this class is also extending to a B demo so let me do that very quickly and now let's say every demo has two more variables and now let's say I want to initialize all the variables who is present inside the demo class during the time up object creation and if I want to do that I need a constructor right so let me remind you that is extends to every demo and every demo is also having this particular variables inside it so again I need to create a constructor and I need to have all this variable inside it so let's go ahead and let's quickly do that you all right so now let's initialize all the variables right so I can write this dot X equal to X this dot y equal to Y and again if you remember I have to initialize this variable as well right so I can write this dot i AQL to I and this dot J equal to J all right so now I want you attention to look at both this class called demo and demo one so if you can see over here in this demo class we actually have two variables right but we are providing the initialization for four variables because these two variables is coming from a B demo class isn't it and similarly if I go for the demo one class then I do have only two variables for demo one class as well which is Y and X right but if you see these two variables called I and J they are coming from a B demo class because demo class I'm in demo one classes extends to every demo and every demo has - two variables isn't it so let's say I have a pipe more classes or a hundred more classes and all the time I need to initialize these two variables over there inside that class right by using this way right I need to write again this dot I equal to I this dot j equal to j in each of the constructor of a particular class isn't it I mean if you can see over here this chord over here in the demo class is also present inside the demo one class as well so we are just writing the same chord again and again isn't it and let's say if we have hundred more classes and all the classes are extending to a B demo class then we have to write this kind of lines over and over again which is taking a lot of time which is time-consuming isn't it and we don't need to write the boilerplate code I'm in the duplicate core again and again in our programming so there is a simple solution and that's why we can have a constructor inside a abstract class so you can see this app store plus has these two variables right so Java is giving you this amazing feature that yes you can write a constructor over here inside your abstract class so actually can write a B demo let's say a B demo and you can get rid of these tops and right now you can actually write into I over here and enjoy over here and you can initialize this abstract class variables over here only so you can write this dot I equal to I and this dot J killed you J right and now I'll put control s and save this particular program then you can see this other two classes that we have written demo and demo one right now is complaining right the constructor is just giving us an error and if we mouse over here then we'll get this morning I'm in the error message that implicit super constructor a B demo constructor is undefined that means it is saying that dude if you already have written a constructor inside your abstract class then you should call it by using the super keyword so that means we don't need to write this particular duplicate cor over here and over here inside this particular class again and again instead so now we can remove this initialization stops over here because this belongs to a B demo class and we have already written a constructor for that so let us use this constructor which we have written inside the abstract class so we can call the constructor called the superclass constructor by writing this super over here and this constructor need a two integer to pass in so I have two already two integer declared over here and these are my local variables I and say so I can write I and J over here and now I'm all set to go so I don't need to initialize all the non-static variables of my superclass which is a B demo class over here one by one instead I can just write a constructor over here inside my abstract class and can use that wherever I need for an example here in my demo one class I don't need this extra lines of code over here instead I can write super and I can just put I and J over here right and there we go and that's why it is very important to write a constructor inside an abstract class okay so there's the importance of writing a constructor inside an abstract class and it can save you by writing a lot of duplicates cord alright so now let's quickly go ahead and run this application just for our testing purpose so I have already did this particular object creation kind of things over here and to taste whether all my variables has been initialized or not I just can print them so I can write this out oops and I can print everything over here by d dot I and we can have a space over here and we can write d dot J and again we can have a space over here and we could type out the rest of the things all right so as you can see I have typed out everything over here so now lists around this particular application right so I'll right-click and I'll run an java application right and there we go so as you can see all my variables whatever I'm passing inside my constructor has been initialized right so similarly if I'll run this particular application or whatever the variable I will initialize for this particular class as well all those things is going to be pop out in my console all right so this is it from this particular tutorial and if you want to know all the differences between the interface and abstract class then you can watch my tutorial by checking out the link in the description and I'll see you there until then happy coding [Music]
Info
Channel: Selenium Express
Views: 37,567
Rating: 4.9129319 out of 5
Keywords: abstract class contains constructor, abstract class example in java, abstract class in java, abstract class in java with example, abstract class interface difference, abstract class program in java, abstract class questions, difference between abstract class and interface in java, need of constructor in abstract class, oops interview questions and answers java, selenium express, use of abstract class in java
Id: jmxOsCGV120
Channel Id: undefined
Length: 12min 35sec (755 seconds)
Published: Mon Nov 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.