What is Super Keyword in Java | How to Use Super
This section is created just so you get a hang of what is Super Keyword in Java and how to use Super whilst coding. Super has nothing to do with Superman, so please stop thinking in that direction. The word Super makes an allusion at the superclass. That’s where the keyword name comes into existence from.
What is Super Keyword in Java
Super keyword is used for overriding scenarios mostly. So, if we use super at a place where the method name or the variable name are same for both superclass and subclass, using super will let the JVM know that you are actually talking about the Superclass.
In the case of constructors, when you try to create an instance of a subclass in Java, an instance of the superclass gets implicitly created too. Alternatively, you can refer to it by using the Super keyword explicitly as well.
Limitations:
Here are the two limitations I came across working on it:
- You cannot call a private method using super.
- You cannot use super in a static context.
I hope all of the above might have answered only a part of “What is super keyword in Java”. To get the complete picture you must see an example take form.
Example to Learn What is Super Keyword in Java
Imagine a superclass and subclass scenario.
Hey now that I think of it, it can be related to Superman somehow. We can geek it up a little to create a Superman superclass which has a subclass Wonderwoman extending it. Why, you ask? Because Superman can fly and so can Wonderwoman. We will create a method called fly that even our Wonderwoman subclass can use.
I am going to go ahead and put this class right up in the Eclipse IDE. So, things are much easier to work with.
I have also created a Wonderwoman subclass that extends Superman superclass. It has the same method as that of Superman and another additional method. Also, it has the same String variable that has been initialized with different things.
To Refer Superclass Instance Variable
I am just going to use super.laser to display the variable in the fly() method.
After that, I am going to create an instance of the subclass and using it call the method fly().
If you try to run the above program, you might get the following result:
This goes on to show that a variable from a superclass can be called using the keyword super. If we would have simply tried to display ‘laser’ we would have received No laser as the output. You can try that!
To Invoke Superclass Method
Now we will try to call the superclass method fly(), using the super.fly() functionality.
The code will look something like this:
I have called the lasso() method where I have put super.fly() to call the fly method from the superclass. Since the fly method of the superclass had “Woosh!” to be displayed we get the result as is.
To Invoke Parent Class Constructor
Apart from the aforementioned usage of the super keyword, it is often brought in use to invoke the superclass constructor.
In a constructor of subclass, the super keyword is implicitly called. You don’t have to mention super() to call it.
In our little example above if we clean things up by removing variables and methods, here is how things would appear:
I have used a “I am Superman” String to be displayed to help identify the constructor.
Our Wonderwoman Class if cleaned will appear something like this:
Here while creating an instance of Wonderwoman in main method super() will implicitly be called. So running the above code will give you:
You can alternatively explicitly call the constructor too. Simply type super() in the subclass constructor.
If you run the above program you will get the same result as above.
NOTE: Remember when you are trying to invoke superclass constructor using super(), it should be the first line in the subclass constructor.
That’s all the places you will probably use super.
Later dude!
2 Responses
[…] Lol! Almost sounds like you are pointing. Okay, this java keyword is nothing but a reference variable just like super keyword that we had seen in the previous chapter. […]
[…] can simply make use of super keyword to that. Read more about super […]