Abstract Classes and Methods - Learn Abstraction in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going you're probably confused about abstract classes right now which is why you click this video but I'm here to tell you that you will not regret that because we're gonna walk through abstract classes and get it working for you but I know for me abstract classes were pretty confusing yeah I'm pretty sure I didn't know what abstract meant when I learned him my name is Alex I make a Java tutorial on this channel every single week so if you're new here and you might be interested in seeing that then please consider subscribing let's start getting an abstract class here by going to file new Java project call it something like abstraction and then hit finish and on the source folder go into new class I'm gonna call it abstract tutorial hit this main method check box and then hit finish an abstract class is basically a bunch of variables and methods that can be used to create other classes so here we can make an abstract class by suing abstract class name is something like dog and what are some values for a dog well we could have like breed or something and what kind of dog do well we could say bark well have it be a normal method and we'll actually implement it too so we could say public void bark method will print out bark like that so we can try to use this abstract class by doing dog D equals near dog and then trying to use it just like any other class a dot bark but it's looking like we got some red underlines and let's see what the error is cannot instantiate the type dog if this was a regular class class dog then you would see that underlines go away and we could do D bark and that would call it bark but as soon as we turn this into an abstract class we cannot use it that's because you can't use an abstract class like a regular class an abstract class helps make regular is that you use there so we can create a new class class Chihuahua Chihuahua class okay and say extends dot so now we can use these through the Chihuahua so we could say Chihuahua D equals new Chihuahua and I'll name it c4 Chihuahua and now if we do that it'll work because now the Chihuahua object is an extension of dog so if it's an extension then it already has what the dog comes with and a little more we just decided not to anything so the things it already comes with is a breed and a bark so we can also print out C breed if we want it to this is no because we're not setting it or using it yet and let's actually get rid of that together because this is the most important part about abstract classes in Java its abstract methods I'll close this up so we can see everything a little better an abstract method is a method that isn't implemented yet so implemented means you put code inside the curly braces we're gonna make an abstract method which means we're not going to put code inside the curly braces so it's the same as before public except we add this new keyword abstract public abstract lloyd bark so we want to do it this way actually we'll change it to poop now we get a red underline under Chihuahua because it needs to now have the poop method since we're extending it we don't have to add the bark because it's already implemented here there's already code it knows what to do but now we have this abstract method which doesn't know what to do when you call it so we have to say what it does when you call it so we can now say public void poop and we want to print out dog pooped okay so now if we do cedar bark and see that poop then we'll see both of those messages so Chihuahua knows how to bark because it extends dog it knows everything inside of here which is why we can call Chihuahua bark it also knows that it needs to poop but it doesn't know what to do since it's not implemented and that's what an abstract method is it's abstract it's an idea it has to have the poop method but it doesn't know what to do so you need to implement it which just means make the method poop and put code in it and that's why it works we could get rid of this abstract method and just add it like this so if we ran it it would still work then you'd be like well why do we even have that abstract method in the first place here well abstract classes help us organize what a dog should have what should this class be able to do so we can just list out and make sure make 100% sure that every single dog has to have this method it might be different for other dogs so that's why we make it abstract so each specific dog can poop a little differently or bark a little differently basically it's saying you have to implement your own poop method but all dogs all these dogs will bark the same is is what this example is so how is this different than an interface an interface is like this you type the interface and say dog interface for example an interface is just a list of variables and methods - so we could have bark and poop would poop but we don't have to put abstract here we can but an interface assumes that all of them will be abstract which means unimplemented here if we try to implement it here just like in the abstract class we try to do that inside of an interface it wouldn't work because look at the message here it says abstract methods do not specify a body why is that message showing up here when the exact same code is right here it's because an interface assumes that all the methods are abstract which means not implemented there's no code inside it's just a list but an abstract class is different because you can have a list of methods and ones that are already implemented so that's the huge difference between interfaces and abstract classes you can't have any implemented methods inside of an interface but you can have both abstract methods and implemented methods inside of an abstract class whew that is a lot of weird words that only programming nerds know I really hope it makes sense to you though if we want to use the dog interface instead of an abstract method we would have to type dog interface and change this from extends to implements because we're implementing the methods from the interface because we know that we're not extending it because extending means adding on to previously implemented methods is that make sense we're implementing all the methods in the interface but we're extending what's already there and also maybe implementing what's in the dog abstract class let me know if this was helpful I wish you the best on your project please let me know if there's anything I can improve on or if I didn't explain correctly thank you have a great rest of your day and I'll see you in the next video [Music]
Info
Channel: Alex Lee
Views: 245,877
Rating: 4.9717855 out of 5
Keywords: abstract classes in java, abstract methods in java, abstraction in java, abstract class java, abstract java, abstract java tutorial, abstract class java tutorial, java abstraction, java abstract class, java abstract class and interface, abstract class vs interface in java, java interface, java abstract methods, abstract method, abstract class, abstract class in java, java tutorial, what is abstraction in java, abstraction example, abstract class example
Id: 52frlN8webg
Channel Id: undefined
Length: 8min 55sec (535 seconds)
Published: Thu Nov 28 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.