Abstract Classes and Methods in Java Explained in 7 Minutes
By Coding with John
Summary
Topics Covered
- Abstract Classes Block Direct Instantiation
- Abstract Methods Force Subclass Implementations
- Abstract Classes Enforce Subclass Contracts
- Interfaces Limit Fields to Static Final
- Abstract for Related Fields, Interfaces for Behaviors
Full Transcript
abstract classes can be a really confusing concept when you're learning java when i was learning they were pretty confusing for me but in this video we're going to go over exactly what an abstract class is how you make one and how you can use one with an example and why you would want to make
one in the first place we'll also talk about the differences between abstract classes and interfaces my name is john i'm a lead java software engineer and i love sharing what i've learned in a clear understandable way so if you enjoyed this video please consider subscribing so you don't miss each new
java tutorial i also have a full java course available in a link down in the description if you're interested so first things first what is an abstract class all an abstract class is is a class that you can't instantiate you
can't create objects from abstract classes so what does that mean well you know if you have like a normal class like this cat class here if you want you can create objects of that class so you
could say cat my cat equals new cat but with abstract classes you can't do this so if i go back into my cat class and make it a public abstract class if i go back into my main method i can see that
now i get an error here cannot instantiate the type cat and by the way this abstract keyword here in the class definition is the only thing you need to make a class into an abstract class probably the first question that comes
to mind is why the heck would i want to do that why would i want to make a class that i can't create objects from so here's a situation where it makes sense to use an abstract class let's change this class back into a regular class
instead of just being a standalone class let's say it extended a class called animal and let's say the animal class had a couple of fields like int age string name it totally makes sense that you could have an animal class that
would have subclasses of actual animals like cat dog horse whatever and it makes sense that you could create objects of those subclasses like you could create a cat object but what doesn't make a whole
lot of sense is creating an animal object you know it's just kind of weird what kind of animal is it so you might want a parent class like animal so that your subclasses can share some fields like age name and maybe some methods
that you write but you might not want to be able to create objects of this animal class all you have to do is make this class abstract so an abstract class is a class you can't instantiate but you can
absolutely make subclasses of an abstract class that can be instantiated now what about abstract methods in any of your abstract classes you can choose to have abstract methods let's say we
had a method like public void make noise of course it makes sense for an animal to be able to make noise but each individual animal is going to make noise in its own way a cat's going to meow a dog is gonna bark so because of that it
might not make a whole lot of sense to actually implement this method make noise here in your abstract animal class what you can do is make this method an abstract method when you make a method
abstract you don't specify a body for the method all you do is declare it and then end it with a semicolon but then in all the child classes of your abstract class you have to actually create an implementation of this make noise method
so if we go back over to our cat class now we can see that it's giving us an error that the type cat must implement the inherited abstract method make noise so of course we can write it all out but
we can also just use eclipse's fix tool here to add the unimplemented method but now this cat class can implement this make noise method however we want print out meow so because this animal class
declares an abstract method make noise any child class of this animal class has to provide its own make noise implementation so now back in our main method we could take my cat and call meow and if we run our program we can
see that it says meow now of course we could in our animal class you know get rid of this abstract method and in our cat class just declare a make noise method that says meow and if we go back
and run our program again it still says meow so why do we need this abstract method what do we want that for what the abstract class does as a whole is kind of enforce and organize exactly what
every subclass of animal has to have so this animal class is saying hey if you want to create a new type of animal it's going to have an age it's going to have a name and it has to be able to make noise every type of animal might make noise in completely different ways but
this makes sure that every single animal type is able to make noise as a side note though in your abstract class all of the methods don't necessarily have to be abstract you can create actual concrete implemented methods in your
abstract classes so you could have like public void print name just prints my name is name so now every subclass of animal will also have this print name method available to it but since it's not abstract they don't need to
implement it themselves they can just use the implementation that's here the question a lot of people have and what's actually also a big interview question is what's the difference between an abstract class and an interface let's
say we had an interface called like animal stuff here in an interface we could say how about public void poop just like every animal might make noise in different ways every animal is
probably also going to poop in different ways in interfaces you don't need an abstract keyword in your methods every method in an interface is assumed to be abstract so you uh you don't need this keyword as you probably know if you want
to implement an interface so all you have to do is instead of extending another class you just say implements the interface which is animal stuff and now that we implement this animal stuff interface we have to
implement this poop method you see right now we get an error that says type cat must implement the inherited abstract method poop we can just click the suggestion here to add the unimplemented method print out
like sounds pretty poopy so just like this abstract make noise method made any subclass implement this make noise method this interface also makes any classes that implement the interface
implement this method they seem like they're doing the same thing right what's the difference the first key difference is that you can implement as many interfaces as you want in java there's no limit but you can only extend
one class and you can totally do both if you want to you can extend the animal class and implement the animal stuff interface another difference is that in interfaces if you declare any fields
like if you had end age and string name every field that's declared inside an interface is going to be static and final that's why i'm getting an error here because it's final i have to instantiate it with some kind of a value
larry because every field in an interface is automatically static that means the same values apply to every object in that class if i say age equals one that would have to apply to every
class that implements this interface so it really doesn't make a whole lot of sense to have fields like this inside interfaces that's why we have abstract classes like this so you can say hey
every animal has to have an age and a name but i'm not going to tell you what it is right now because that doesn't make sense each individual cat object dog object and horse object can all have their own age and name values so you can
do that with an abstract class but not with an interface so you might want to create an abstract class if you have a lot of closely related classes that you want to have the same functionality and the same types of fields available but
you might want to make an interface instead if you have a lot of unrelated classes that you all want to be able to do a certain thing that makes it so you can guarantee that other types of classes will be able to poop even if
they aren't animals that might be the weirdest sentence ever said in a java tutorial if you have any questions or clarifications let me know in the comments or if i got something wrong feel free to shout and rant at me
angrily thanks so much for watching and i'll see you next time
Loading video analysis...