TOP 6 difference between Abstract class and Interface in JAVA || Must know facts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everyone is a real aspect here again from selenium Express and today in this tutorial I'm going to talk about the difference between the interface and abstract class this is very popular interview question and doesn't matter your refresher or doesn't matter you are an experienced guy but 90% of time you are going to get this question in your interview so let's go for it and let's talk about it in a brief all right so now let's discuss about the difference between the interface an abstract class so if we talk about this interface right I have created a very simple interface called a and if you talk about this interface a this is preceded by this new word called interface isn't it so whenever you are creating an interface that interface should be preceded by this keyword called interface and similarly whenever you are creating any abstract class let's say this is the abstract class over here right and this flash should be visited by the keyword called a strap [Music] so this is a very common difference so whenever you are creating an interface you have to keep in mind that that interface should be preceded by this keyword called interface and whenever you are creating an abstract class you have to keep in mind that that abstract class and in that class should be preceded by this new word called abstract isn't it so said very common things so now let's talk about when should we go for interface and when we should cover abstract class service in the stand [Music] alright guys so now let's understand when should we go for abstract class and when should they go for an interface ok so now let's say let's understand it this way let's say you want to build an app let's say this is an app ok you want to build this up and let's say this is an employee up let's say EMP right and whenever you want to build this up you want to methods to be there inside this particular app let's say you want a method called get salary info and you want a method over here called get EMP info right so you want these two methods to be present inside your app right so now let's say here what is happening you know that you are going to develop these two matters right but you don't know the implementation you don't have any logics right now you don't know how to develop it what you know is that you are going to develop these two particular methods right you only know the method name you do you wanna know the specification right so in that case you need to go for interface so now the same here in this case I have two methods right castellari input so I can write a method over here so let's say if this interface is my employee interface let me write it employee over here so my interface name is EMP so now I do have two methods over here or less they get salary info and I have another method this says get EMP info all right and you know that all the interface method is by default public and abstract right so I don't need to write that it's by default public and abstract and is by default public and abstract so what we understand from here is that whenever you only know the specification but you don't know the implementation you don't know how to develop this particular methods you don't have any notice right now in your mind at that time you can go for interface all right [Music] so now let's say when you should go for abstract class okay so now let's say if you know about this particular matter you know the implementation of one method let's say you know how to calculate the salary in power how to create this particular method called salary info but you really don't know how to create this particular method called care and pro info so you know the partial implementation right you know how to develop this method so you only know how to create one method but you don't know how to create this particular method so the thing is the implementation is not so clear for you right but you know the partial implementation at the end time you should go for abstract class so let's say right now you know this particular method how to develop it okay so let's say you have this method called get salary input and you know how to develop it so it has a body Leste and you have something over here some core of it you know you've written right so this is the implementation part and let's say you have an end of the metal let's say get employee info right and you really don't know how to develop this particular method right so this is an abstract method because it doesn't have a body right I have given the semicolon over here so let's say this method is my abstract method so I can write you over here that public and abstract because this matter I'm not providing any body for this method right I don't know the implementation for this particular method right so I have to make it abstract right so this method is abstract but here I have given the implementation so this is a concrete method right so the thing that we understand from here is that an abstract class can contain both concrete method and abstract method right [Music] okay so you should have got right now if you know about the interface right the people or the person or anybody who is going to implement your interface they need to going to provide the implementation for all your methods right so you should look about right now you have two different methods over here somebody this is somebody has a class listed class a and if you need to implement your interface less the implements the say implements EMP then he needs to override all these particular matters right he needed to provide the body for all your methods right and similarly if we talk about the abstract class let's say if I have a class called X and if I am extending this particular class a over here so I have to write X extends a right and inside this I have to keep the implementation for all the abstract method so if you talk about this particular class called a then I do have one abstract method right which is get employee info so if my X class is extending the a class then I need to provide the implementation for this particular abstract method right so I need to offer I this particular method over here and I need to give it a bad right and we have discussed that in our after class tutorial right okay so what we understand from here is that you have to go for interface when you only know the specification but you don't know the implementation and the time you have to go for interface but you put about the abstract class then you have to go for the abstract class when you know the partial implementation you know some methods implementation right but not the all at that time you have to go for abstract class then the methods what you don't know the implementation you can make it abstract so that when somebody is extending your class he has to provide the implementation for those particular methods right okay so now let's talk about in another difference and this is really really important let's say your interviewer may make you confused he may ask you some confusing questions like this let me remove this thing okay so now let's say that your interviewer may ask you this okay so this is your interface right so if you know that from Java 1.8% we can actually write your functional interface and functionally interface means you know we can put the functions inside our interface right so if we talk about this particular method let's take at employee info if I want I can give it a party right I can put some implementation over here and I can make this method default method so let's say this is my default method right so if you have more the default method inside interface by using the default method you can give the body to a method inside an interface from the more 100-person you actually can have concrete method an abstract method inside your interface right so now let's understand so let's say if your interface can have concrete method an abstract method inside it and your after class notes are helping the concrete method and the abstract method inside it then what is the difference and when should you go for abstract class and when should you go for interface so this is this is kind of same right so when should it go for abstract class adventure to go for interface so let's understand that so now let's say if you talk about interface right so let's say if I have the class called so let's say if I have a class called a right I can implement if I'm implementing this interface let's say EMP so let's say my class a implements EMP then this fine by this force fine and let's say if I do have another interface right so I could have ended at another interface let's say EMP one all right so I can just put it come over here and can make it EMP one right so my one class can implement any interface right any number of interface there is no problem with that right but if you talk about here the abstract class you cannot write let's say if you have a class called let's say X and now say this x+ is extending this a class right so I can say X extends a right and that is say if I have another abstract class called B and I want to extend that abstract class also over here with my a class so can I do that can I say X class extends a comma B it's not possible why because Java does not support multiple inheritance so if you talk about interface by using interface we can achieve multiple inheritance right but if we talk about abstract class by using abstract class we cannot achieve multiple inheritance right so there's no another difference that you have to understand you can implement any number of interfaces you want but with abstract class if you are extending to abstract class you can only extend to one after class you can extend to only one class at a time not more than that [Music] so this is one of the another difference between the interface and after class right and then this point right here let's say I okay and if I'm taking a variable over here inside the class is where let's say in time all right then what is the difference so if we talk about the variables inside the interface all the variables are by default public static and final so all the variables of interfaces by default public static final even if you are not writing anything upper here the Salem is going to automatically make it public static and final right but what about the variables inside the abstract class so the after class variables modifier can be anything there is no such restriction with this right you can make it public you can make it private you can make it anything any varied body fire you can put it before the variable of abstract class all right so this is a one word differences right the variables inside the interface should be perfect static file the variable inside the abstract class should not be public static final I mean it can be anything right so there is no such restriction all right so you can make anything that you want but now here a very important question arise so let's understand that alright so here you can see you put about the variables inside an interface right so this variable is by default final this public static final right and if you are writing as final variable and if you are not initializing it the compiler is going to complain right so whenever you are writing a variable inside an interface you should initialize it so in this case I can initialized to anything right so let's left pane right so the initialization is compulsory you could have a power interface but if you talk about abstract classes you you may not initialize it you may initialize it it's up to you right all right so this is one of the another differences if we talk about the variable of interface and variable about [Music] all right so now let's understand one of the another most important difference between interface and an abstract class so the difference is the interface cannot have any constructor within it but the after class can have a constructor within it now let's understand why okay so if you want to gather all the details about it then you can check a link on the description okay I've already by the end of the tutorial for that you can go and watch that over there but here less understand it in a simpler way okay so you put over the interface right so the older interface variables are by default static right so we know that when we need a constructor we need a constructor whenever we need to initialize the variables which variables the not chatty variables right so whenever we want to initialize the non static variables of a class at that time we should cover the of instructor isn't it so you put a point over here inside the interface there is no non static variables are there right and we don't in the constructor for that isn't it but if you don't bother after classes here we have non static variables right and I can initialize this non static variables whenever I am creating the object and I can initialize this non-static variable when I'm creating the object of the scene also implemented subclasses right so what I mean by that I can put a suppose my class name is a over here I can I can put a constructor over here like this right I can make it public or something right so I can write a constructor inside an abstract class but I cannot write any constructor within the interface right all right so just understand one thing that you cannot create any object both for interface an abstract class right you just did not create any object for it so a lot of people thinks like they can create object for abstract class indirectly let me tell you that by directly or indirectly you just cannot create any object for abstract classes thing think about it okay after class doesn't have any implementation right so it's partially partially implemented you can say so if it is partially implemented that means it doesn't have a complete implementation so if so if this class is abstract and doesn't have a complete implementation how can you create outfit for that so this thing you have to keep in mind that for after class and interface you cannot create any object right [Music] so these are the few differences between interface and Napster class that I want to cover in this tutorial if you know any other differences you can comment that pillow in the coming session and I'll be glad to read that so this is it for today and this is not in goodbye I'll see you somewhere in my later video and for that please do subscribe to my channel and kill that happy [Music]
Info
Channel: Selenium Express
Views: 145,783
Rating: 4.8049765 out of 5
Keywords: slenium express, difference between abstract class and interface, what is interface in java, interface vs abstract class in java, what is abstract class in java, when to use abstract class and interface in java, interface and abstract class in java, abstract class vs interface java, what is the use of abstract class in java, difference between interface and abstract class in java, difference between abstract class and interface in java, interface in java, abstract class in java
Id: Ud0zfImgbYw
Channel Id: undefined
Length: 17min 47sec (1067 seconds)
Published: Thu Nov 30 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.