Super Keyword in Java Full Tutorial - How to Use "super"

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to talk all about the super keyword in java super is one of those keywords that people kind of think they understand how it works but maybe you're not quite clear what exactly it's doing and when you need to use it by the end of this video you'll know exactly what you can do with the super keyword and when and how to use it my name is john i'm a lead java software engineer and i love sharing what i've learned with you in a clear and understandable way i also have a full java course available in the link down in the description if you're interested so let's get to it alright so first things first what is the super keyword used for in general super is used to access things in the parent class the super class of the class you're working on hence the name super so for example if you have a class like this animal class that we have here you might have a class that is a sub class of animal for example a cat class this cat class extends animal so animal is the super class of cat so within the cat class you'll use the super keyword to access things in the animal class there's two main ways you can use the super keyword in your classes the first way that you can use super is to call methods in the super class that you've overridden in the child class so what exactly does that mean why would you want to do that well for example over here in our animal class we have this method public void make noise and all a generic animal does to make noise is print out hello i am an animal seems like the kind of thing an animal would say now the cat class which extends animal would probably make noise in its own specific way so what we might do in our cat class is override the public void make noise method so cats will make noise in their own way so for cat we can print out meow meow meow as a side note because this method overrides the make noise method in the parent animal class although it's not necessarily required it's good practice to use the at override annotation here so now if we go back to our main method and create a new cat my cat equals new cat and call my cat dot make noise it's going to print out meow meow meow but what if inside the cat classes implementation of the make noise method i want to be able to call the parent animal classes version of the make noise method how can i do that well that's where the super keyword comes in so over here in our cat classes make noise method to call the super classes implementation of that make noise method you just use super dot make noise so now when we call the make noise method on a cat it first calls the parent classes implementation of the make noise method which should print out hello i'm an animal and then continues on and prints out meow meow meow and if we go back and run our program again that's exactly what it does you don't necessarily have to be in the child classes implementation of the make noise method in order to call the make noise method in the parent class you can make the same call from any non-static method in this class so for example if we had another method like public void jump you can still call the super classes make noise method from here just as you could over here but in most real world scenarios you'll see that superclass method being called inside the method that is overriding it in the subclass but it's good to know that you're not prevented from doing that if you need to you can also use super to call parent class methods that you don't override in your subclasses but as we'll see there's not really much reason you'll ever need to do that like here in our animal class it has this public void eat method that just prints out munch munch but in the cat subclass we aren't overriding that eat method with anything but since cat is a subclass of the animal class it automatically gets this implementation of the eat method so even though we're not overriding the eat method we can still call my cat dot eat so now it's also printing out lunch munch so in the cat class in any method that we want we can call super.eat if we want to that will of course call the superclass the animal classes implementation of the eat method but in this situation we don't really have to use super we can just call eat and because our cat class isn't overriding the super class's implementation of the eat method by default just calling eat will get us the animal classes implementation of the eat method that's why you only really need to use the super keyword when you specifically want the parent classes implementation of a method that you have overridden in your subclass there are a couple things to note about this though the first thing is you can't just do this from some other random class for example back here in our main method you can't just say hey my cat i want your super class's implementation of the make noise method using something like mycat.super.makenoise it's just not a thing the super keyword can only be used inside a class to refer to that class's super class it's parent class the second thing is that you can't use the super keyword to access any private methods or fields in the parent class since they're private they're still only going to be accessible to that parent class and not anywhere else even if you do use super so if in our animal class we had another method private void do something private hey this method is private if we then go back over to our cat class and try and call super dot do something private we'll get an error that says this method is not visible and that's because it is still private to the animal class but if this method happens to be either public or protected then you're good to go and you'll be able to access it from the subclass using super the second way that you can use the super keyword is to call the parent classes constructors but that seems a little weird right why would you want to do that let's look at an example let's say that here in our animal class we had a new constructor public animal now this animal class has two fields an int age and a string name so in our constructor maybe we want to take in those two values and set them on the object that's being created so we can take in an ant age and a string name and to set those two values we just want to call this dot age equals age and this dot name equals name by the way you can check out this video here if you want to learn all about using the this keyword it'll clear up any questions you might have about how to use this anyway so this constructor allows us to set the animal's age and name when it's being created now over in our cat class we probably want a similar type of constructor right that can take in this cat's name and its age but in our cat class we also have a field that doesn't exist in the animal class because it's specific to cats and that's a string cat food preference field so in our constructor we probably want the ability to set that when the object is being created too so we can create a new constructor public cat that takes in those three fields and sets them on the new cat being created so it takes in an int age string name and a string cat food preference and we can set those three fields in the same way this dot age equals age this dot name equals name and this dot cat food preference equals cat food preference so now we have a constructor that takes in all three of these fields and sets all three of them on the new cat being created but if we look closely there's kind of a little bit of code duplication happening here right so here in our cat constructor we're setting the age and the name that's being passed in but in our animal class we already have a constructor that's doing that so it would be nice if we could use this animal constructor for setting the first two values on our cat and then we just need to set the cat food preference on top of it well the super keyword allows you to do exactly that we can use the super keyword to call the parent classes constructor from here and to do that you just call super and then in parentheses you pass in whatever parameters are required for the superclass constructor that you want to call here we want to call this constructor that takes in the int age and string name so all we have to do is pass in age and name the superclass constructor that we're calling here this animal constructor will take care of setting the age and the name and we only have to worry about setting additional values that are specific to cats which here is just cat food preference using super like this it will call the super classes constructor that matches the parameter types that we pass in but if you try to pass in types that the parent class doesn't have a constructor for like if we added i don't know another string like john the parent class doesn't have any constructor that takes in an int a string and a string so you'll get an error here so now let's try calling this new cat constructor that we made so we need to pass in an age a name and a cat food preference the age is three the name is george and the cat food preference is purina one to make sure those values are being set correctly we can go ahead and print them out mycat.age mycat.name plusmycat.cat food preference and of course we have a three-year-old cat named george who prefers purina one now there are a couple of things you need to know about using super like this to call the superclass's constructor methods the first is that calling the superclass's constructors can only be used inside the subclass constructors it doesn't make any sense to do it in any other method so if you try to do it you'll get an error also when you do use it in a constructor it has to be the very first line of that constructor so if you try to just switch these two lines around and have it set the cat food preference before calling the super class constructor you will get an error saying that the constructor call must be the first statement in a constructor by the way one of my favorite shortcuts for eclipse is pressing alt and the arrow keys to move lines of code up and down i think that's pretty awesome and you can check out this video here to see even more of all the very best eclipse shortcuts to speed up your programming the second thing is not very many people know this but at the very beginning of every constructor method if you don't have any call to super like this java will actually call the parent classes no args constructor implicitly and automatically without you having any code or anything so if you don't have anything like this it's as if you're calling the superclass constructor with no parameters you can always put in the supercall in your constructors if you want but you absolutely don't have to java will do that automatically but that does mean if you want to use any of the other superclass constructors you have to explicitly call it like we did here with the parameters that you want for that constructor otherwise by default it will automatically use the parent classes no args constructor also in that situation where you don't have any specific call to a superclass constructor and it automatically uses that parent class's no args constructor if there isn't a no args constructor for that parent class you're going to get an error so if we go back to our animal class and get rid of this no args constructor and then go back to our cat class now you can see we have an error here because implicitly it's calling that parent classes no args constructor and you get an error because it doesn't exist so in this situation you can either call some different parent class constructor like we did here or you can actually implement that no args constructor in your parent class but that is an error that can be really really tricky to hunt down and understand if you aren't familiar with this so keep that in mind to prevent yourself from banging your head against the wall when you see that type of error in the future if you enjoyed this video or learned something please let me know by hitting the like button and be sure to subscribe so you don't miss each new java tutorial and don't stop now keep up the momentum and check out one of these other videos below to keep on learning as always thank you so much for watching and i'll see you next time
Info
Channel: Coding with John
Views: 2,501
Rating: undefined out of 5
Keywords: java, codingwithjohn, coding with john, java beginner lesson, super, java super, super java, java super keyword, super keyword java, java super keyword example, java super keyword in constructor, java super constructor, java super method, java super method call, super java constructor, super keyword, super keyword in java, call superclass method python constructor java, java call super constructor, java call super method
Id: Qb_NUn0TSAU
Channel Id: undefined
Length: 11min 33sec (693 seconds)
Published: Mon Oct 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.