Abstract Classes vs Interfaces (Java)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the code heart truth fighting bad programming one error at a time alright welcome in so today what we're going to be talking about are the main differences between an abstract class and an interface and the best way to look at both of these generally is to see them both as contracts so any subclass that's gonna inherit from either an abstract class or an interface is gonna have to implement some type of method and the method that it's gonna have to implement are called abstract methods all an abstract method is is a method that doesn't contain a body so taking a little bit closer look here the main difference between an abstract class and an interface an abstract class is simply just the class that usually contains at least one abstract method now there are certain instances where an abstract class could have absolutely no methods in it and that's usually done I in order to prevent a class from being instantiated because abstract classes cannot be instantiated but don't worry about any of that because nine times out of ten when you see abstract class chances are it's gonna have abstract methods in it as well as regular methods that you would find in any normal class where an interface contains only abstract methods and interfaces also cannot be instantiated so I know this kind of sounds a little bit vague but let's get a better understanding of exactly when we should use this why we should use this by using an example that's gonna one use an abstract class and then it's gonna be the same example that uses an interface and hopefully you'll be able to see the difference and get a better understanding of this concept so our example is gonna be centered around early to late 90s wrestling superstars because widgets are boring so let's start off with looking at an abstract class so this is gonna be our abstract parent class that all of the wrestlers will inherit from so if you look at this class it's a regular class it's gonna have a regular general method in it and it's gonna contain some abstract methods so if there's a mixture of unique and generalized methods that your subclasses will inherit then you should use an abstract class as the superclass now what does that mean let's take a look at this even closer let's say for example that every single wrestler it makes the same amount of money every time they step into the ring doesn't matter if you're a superstar or if you're just starting out so that's a general method if we if this was just a regular class it would look like this and when we instantiated the objects we'd be able to use this method and it would tell us how much that wrestler made for the match but wrestlers aren't all the same they're unique they're gonna have some aspects to them that stand out for example they're all gonna have different theme music they're all gonna have different finishers so the best way to implement what we're talking about is by creating these abstract methods that every single wrestler aka subclass when they inherit this wrestler superclass are gonna have to implement these methods for example let's say we make a wrestler class called Kane we extend wrestler because again an abstract class is just the class when you're inheriting from a class you're gonna have to extend it and when you do that initially you're gonna get this syntax error it's gonna tell you hey there's a contract here that you're abiding by by extending this wrestler class and what the contract is is that you're gonna have to add these abstract methods so if you click at unemployment the methods it's gonna create the two methods that we had early in our superclass these methods are specific to the wrestler so I went ahead and filled them out when we use the theme music Kane's interim uses gonna play when we use the finisher method his finisher is gonna get printed which is the tombstone so we went ahead and created the second wrestler same exact thing it's he's gonna have a couple of these methods that are unique to him they're all in common like every single wrestler is gonna have a finisher half theme music but they're all gonna be a little bit different so it's smarter to create an abstract class where your subclasses will use a general method like this to find out how much each wrestler makes per match but then also have once again unique methods that are gonna be specific to the subclasses now why do we do this you could technically go into each individual subclass and type out you know public void theme music and make its own method and then the same thing for a finisher but let's say you had a hundred different wrestlers that would be a pain in the ass to do so it's just simpler to have these abstract methods in the superclass when the subclass is inherent it's gonna force you to create and override your methods and then you can make them unique to each individual subclass so let's see this in action this is our client module we created two wrestlers one's gonna be Cain one's gonna be stone cold and here we utilize all of the methods that came with the parent class the first one is gonna be the theme music the finisher which again are both abstract methods that are unique to the wrestler and then we're gonna have this generalized method which is just gonna tell us how much money they made for the match so wrestler one is Cain wrestler two is stone cold and when we hit run let's see what we get cool so wrestler one who is Cain again is utilizing his abstract method we made it unique to that subclass uh it's gonna play Cain's intro music you finishers the tombstone and then we use just the regular method that's part of that class that tells us how much money they made Kane worked five hours at 250 an hour he made 1250 for the night uh same thing happened with stone cold unique unique and then he made 750 bucks because he wrestled for only three hours so in order to make this class abstract you have to use this keyword ah if you don't use it you will get a syntax error and it's gonna say well hey dude you have abstract methods here like this needs to be an abstract class now what is an interface an interface using the same example here is almost identical to an abstract class only think of it as a more hardcore contract basically there are no methods that contain bodies every single method in your interface is gonna have no body in it now when's a good time to use an interface let's say each of your subclasses has again common methods that they're gonna be using but are gonna be absolutely unique to each class then using an interface is a good idea for example let's say each wrestler now they make different amounts of money let's say stone cold makes more money than cane when he wrestles so you're basically gonna create these methods and implement this interface in your subclass because an interface isn't a class so you can't extend it and when you implement it what you're saying is hey this is a contract between this subclass and this interface and the contract states that you have to add all of the abstract methods in there so there they are and again I already filled these out so I'm just gonna hit back back I have Austin make it 300 bucks an hour I have Cain making 200 bucks an hour here and when we go to our client module we'll do the same exact thing here we're just gonna instantiate these objects we're gonna use the methods provided to us by the interface and when we run it we're gonna see a little bit of a different result because now this payment for work method doesn't have a body in it and it's gonna be customized to each class so Kane who wrestled for five hours makes 200 bucks an hour will make a grand and Austin who also wrestled for five hours makes 300 bucks an hour so he'll make 1,500 so put simply an abstract class is basically just the class that usually contains at least one abstract method and it's a contract if you inherit it then you're gonna have to implement those abstract methods within your subclass and an interface contains just abstract methods it isn't a class it is a contract if you implement it if you inherit from it then you're gonna have to override all of the methods that are contained within that interface so hopefully that makes sense if you have any questions leave comments below and that is it for this lesson thank you for watching
Info
Channel: The Code Hard Truth
Views: 138,083
Rating: 4.9026346 out of 5
Keywords: java, programming, abstract class, interface
Id: 2aQ9Y7bumts
Channel Id: undefined
Length: 9min 43sec (583 seconds)
Published: Wed Dec 21 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.