Java Anonymous Inner Classes Explained in 6 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to talk all about anonymous inner classes in java and the question on everyone's mind how they relate to bigfoot my name's john i'm a lead java software engineer and i love sharing what i've learned in a clear and understandable way so be sure to hit the subscribe button 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 alright so anonymous inner classes this is another one of those things in java that just has a scary sounding complicated sounding name anonymous inner classes it sounds really hidden and shady and weird but they're really not all that complicated and they can be really useful and really cool to use an anonymous inner class is a class with no name that you use to instantiate only one object ever so whenever you use an anonymous inner class you'll always be defining that class and instantiating the single object of that class at the same time in the same java statement this will become a whole lot clearer with an example your anonymous inner class can either extend any existing class or implement any existing interface we'll start with the one that extends another class so let's say we have this type um animal and we want to create a new object of type animal we'll just call it my animal so animal my animal equals new animal just a basic constructor to create a new animal object right and if we hop over to this animal class we can see that it just has one public method called make noise that just prints out yep yep so of course if we took this my animal object and called the make noise method on it of course it would just print out yappy app what if we wanted to create a new type of animal object like a bigfoot that there is only ever going to be exactly one instance of and we want it to be able to make noise in a completely different way than a regular animal object well we can use an anonymous inner class to create our bigfoot so here's how you do it it starts out looking like any other constructor call animal bigfoot equals new animal however if we leave it like this we get a basic plain old animal object if we wanted to find our own new type of animal here we just have to put an open and close curly braces and inside these curly braces are where we put our new classes definition so what we're actually doing here is creating an anonymous subclass of the animal class and writing that class definition right here between these curly braces so in the animal class we have this make noise method right well as a part of this anonymous class definition we can override this make noise method with our own implementation public void make noise now of course we want to have it make noise like a bigfoot would system.out.printline growl perfect sounds exactly like a bigfoot so now if we take this bigfoot object and call make noise on it this regular animal object my animal still says yappy happy app but our bigfoot says this is what anonymous inner classes allow you to do they allow you to create an unnamed subclass of some other class like animal and create one single object of that anonymous class so of course it makes sense to use an anonymous inner class to create a bigfoot object you know a bigfoot object is mysterious you can't go around as a bigfoot object with a big old you know class name of bigfoot on your forehead and you can only ever create one of them we created this one-time use anonymous inner class as a subclass of animal so now later on down in the code because this class that i made doesn't have a name i can't just instantiate another object of that class it's a one-time use class i mentioned that there are two different ways to create an anonymous inner class and one creates a subclass of an existing class like we did here another way is by implementing an interface but the concept is the same you create a one-time use class that implements an interface that you specify one interface that you might be familiar with is the runnable interface you use the runnable interface especially when you want to create a multi-threaded program by the way click up here if you'd like a great video on how to use runnable to create a multi-threaded java program but usually when you want to use a runnable to create a multi-threaded java program you have to create a whole separate class file that implements the runnable interface and then you can finally create an object of that type and get your new thread running instead you can use an anonymous inner class to make it so much easier to do that we can say runable like my anonymous runnable equals new runnable now of course java is giving us an error here because we can't instantiate an interface runnable is an interface it's not a class so what we need to do is create an anonymous inner class here that implements the runnable interface so we'll do the same thing we did above and put in our open and closed curly braces now java knows that we're trying to create an anonymous inner class that implements the runnable interface and it suggests to us here that we need to add the unimplemented run method to properly implement that runnable interface so let's click it and do it when you implement the runnable interface you have to add your own implementation of the run method so that's all we have to do here so we can do whatever we want we can just have it print out prime and anonymous runnable it's good practice to have this at override when you're either implementing an interface method or you're overriding a parent classes method so we should probably also go back and add this at override annotation to our make noise method because we're overriding the animal classes method here but back to our runnable it might look like here that we're creating a new object of a type runnable but we're not remember you can't create an object from an interface you can only create it from a class so even though it looks like we're creating an object of a type runnable we're really creating an object of a class type that doesn't have a name that we're creating here and that's the same up here bigfoot isn't technically an animal object it doesn't have the type animal its type is this anonymous subclass of animal that doesn't have a name but now we can take this my anonymous runnable object and call run on it if we want run that and we get our yap yap yap from our animal our from bigfoot and i'm an anonymous runable from our anonymous runable object and again check out that multithreading video if you want to be able to use your anonymous runnable objects to create a multi-threaded java program so an anonymous inner class can be really useful if you want to create a subclass of some other class and you only ever need one object of the subclass that you're creating like we wanted to create bigfoot as a new type of animal that makes noise in a different way but we'll only ever need one bigfoot or if you need a single object that implements an interface and you don't have to go and create a whole other class file to create the class that implements the interface you can just do it right here in one single statement and get the object that you need if you like this video or learned something please let me know by leaving a like and if you'd like to do me a huge favor you can do the youtube trifecta of leaving a like leaving a comment and of course hitting the subscribe button you guys are doing all of that really does help the channel and get these videos out to help more people so i really do appreciate it alright thanks for watching i'll see you next time can't believe i just did that
Info
Channel: Coding with John
Views: 1,958
Rating: undefined out of 5
Keywords: anonymous inner class, anonymous inner class java, java anonymous inner class, anonymous inner class java 8, anonymous inner class tutorial, java beginner tutorial, java, coding with john, codingwithjohn, java anonymous class, anonymous class, anonymous class tutorial, java beginner, java tutorial, java anonymous class tutorial, anonymous inner class in java, java tutorial for beginners, java bigfoot, bighfoot anonymous class, java inner classes, inner class, java inner class
Id: DwtPWZn6T1A
Channel Id: undefined
Length: 6min 27sec (387 seconds)
Published: Mon Jul 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.