- What is super type java
- What is super type java
- 2) super can be used to invoke parent class method
- 3) super is used to invoke parent class constructor.
- Note: super() is added in each class constructor automatically by compiler if there is no super() or this().
- super example: real use
- Feedback
- Help Others, Please Share
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- What is Super Keyword in Java | How to Use Super
- What is Super Keyword in Java
- Limitations:
- Example to Learn What is Super Keyword in Java
- To Refer Superclass Instance Variable
- To Invoke Superclass Method
- To Invoke Parent Class Constructor
- Share this:
- Using the Keyword super
- Subclass Constructors
What is super type java
- Introduction to Java
- The complete History of Java Programming Language
- C++ vs Java vs Python
- How to Download and Install Java for 64 bit machine?
- Setting up the environment in Java
- How to Download and Install Eclipse on Windows?
- JDK in Java
- How JVM Works – JVM Architecture?
- Differences between JDK, JRE and JVM
- Just In Time Compiler
- Difference between JIT and JVM in Java
- Difference between Byte Code and Machine Code
- How is Java platform independent?
- Decision Making in Java (if, if-else, switch, break, continue, jump)
- Java if statement with Examples
- Java if-else
- Java if-else-if ladder with Examples
- Loops in Java
- For Loop in Java
- Java while loop with Examples
- Java do-while loop with Examples
- For-each loop in Java
- Continue Statement in Java
- Break statement in Java
- Usage of Break keyword in Java
- return keyword in Java
- Object Oriented Programming (OOPs) Concept in Java
- Why Java is not a purely Object-Oriented Language?
- Classes and Objects in Java
- Naming Conventions in Java
- Java Methods
- Access Modifiers in Java
- Java Constructors
- Four Main Object Oriented Programming Concepts of Java
- Inheritance in Java
- Abstraction in Java
- Encapsulation in Java
- Polymorphism in Java
- Interfaces in Java
- ‘this’ reference in Java
What is super type java
2) super can be used to invoke parent class method
The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overridden.
In the above example Animal and Dog both classes have eat() method if we call eat() method from Dog class, it will call the eat() method of Dog class by default because priority is given to local.
To call the parent class method, we need to use super keyword.
3) super is used to invoke parent class constructor.
The super keyword can also be used to invoke the parent class constructor. Let’s see a simple example:
animal is created dog is created
Note: super() is added in each class constructor automatically by compiler if there is no super() or this().
As we know well that default constructor is provided by compiler automatically if there is no constructor. But, it also adds super() as the first statement.
Another example of super keyword where super() is provided by the compiler implicitly.
animal is created dog is created
super example: real use
Let’s see the real use of super keyword. Here, Emp class inherits Person class so all the properties of Person will be inherited to Emp by default. To initialize all the property, we are using parent class constructor from child class. In such way, we are reusing the parent class constructor.
For Videos Join Our Youtube Channel: Join Now
Feedback
Help Others, Please Share
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
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.
Share this:
Using the Keyword super
If your method overrides one of its superclass’s methods, you can invoke the overridden method through the use of the keyword super . You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass :
Here is a subclass, called Subclass , that overrides printMethod() :
public class Subclass extends Superclass < // overrides printMethod in Superclass public void printMethod() < super.printMethod(); System.out.println("Printed in Subclass"); >public static void main(String[] args) < Subclass s = new Subclass(); s.printMethod(); >>
Within Subclass , the simple name printMethod() refers to the one declared in Subclass , which overrides the one in Superclass . So, to refer to printMethod() inherited from Superclass , Subclass must use a qualified name, using super as shown. Compiling and executing Subclass prints the following:
Printed in Superclass. Printed in Subclass
Subclass Constructors
The following example illustrates how to use the super keyword to invoke a superclass’s constructor. Recall from the Bicycle example that MountainBike is a subclass of Bicycle . Here is the MountainBike (subclass) constructor that calls the superclass constructor and then adds initialization code of its own:
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear)
Invocation of a superclass constructor must be the first line in the subclass constructor.
The syntax for calling a superclass constructor is
With super() , the superclass no-argument constructor is called. With super(parameter list) , the superclass constructor with a matching parameter list is called.
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object . In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.