- super and this keywords in Java
- Java
- super keyword
- Java
- Similarities in this and super
- Java
- Java
- Java
- Can we use both this() and super() in the same constructor?
- Using the Keyword super
- Subclass Constructors
- Super in java methods
- 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
- Super in java methods
super and this keywords in Java
In java, super keyword is used to access methods of the parent class while this is used to access methods of the current class.
this keyword is a reserved keyword in java i.e, we can’t use it as an identifier. It is used to refer current class’s instance as well as static members. It can be used in various contexts as given below:
- to refer instance variable of current class
- to invoke or initiate current class constructor
- can be passed as an argument in the method call
- can be passed as argument in the constructor call
- can be used to return the current class instance
Java
super keyword
- super is a reserved keyword in java i.e, we can’t use it as an identifier.
- super is used to refer super-class’s instance as well as static members.
- super is also used to invoke super-class’s method or constructor.
- super keyword in java programming language refers to the superclass of the class where the super keyword is currently being used.
- The most common use of super keyword is that it eliminates the confusion between the superclasses and subclasses that have methods with same name.
super can be used in various contexts as given below:
- it can be used to refer immediate parent class instance variable
- it can be used to refer immediate parent class method
- it can be used to refer immediate parent class constructor.
Java
Similarities in this and super
- We can use this as well as superanywhere except static area. Example of this is already shown above where we use this as well as super inside public static void main(String[]args) hence we get Compile Time Error since cannot use them inside static area.
- We can use this as well as superany number of times in a program.
- Both are non-static keyword.
Java
See above we have used this 3 times. So this can be used any number of times.
Java
See above we have used super 2 times. So super can be used any number of times.
Note: We can use ‘this’ as well as ‘super’ any number of times but main thing is that we cannot use them inside static context.
Let us consider a tricky example of this keyword:
Java
//it is of S.O.P(first) of garcia method 22 //it is of S.O.P(second) of garcia method 33 //it is of S.O.P(a) of garcia method 22 //it is of S.O.P(b) of garcia method 33 //it is of S.O.P(first) of louis method 1 //it is of S.O.P(second) of louis method 2 //it is of S.O.P(m) of louis method 1 //it is of S.O.P(n) of louis method 2
Flow of program : First, it start from main and then we have new RR().garcia(100, 200) then flow goes to garcia method of RR class and then in that we have:
a = this.first b = this.second
Here, what is happening is that the value of instance variable(i.e, first) and the value of static variable(i.e, second) are assigned to the garcia method’s local variables a and b respectively. Hence value of a and b comes out to be 22 and 33 respectively. Next we have new RR().louis(1, 2) hence here flow goes to the louis method of RR class and in that we have:
this.first = m this.second = n
Here, above what happens is that the value of the louis method’s local variables i.e, m and n are assigned to the instance as well as static variables i.e, first and second respectively.
Hence, the value of first and second variables are same which we have passed into the louis method i.e, 1 and 2 respectively.
Can we use both this() and super() in the same constructor?
An interesting thing to note here is that according to Java guidelines, this() or super() must be the first statement in the constructor block for it to be executed. So we cannot have them together in the same constructor as both need to be the first statement in the block for proper execution, which is not possible. Trying to do so would raise a compiler error.
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.
Super in java methods
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
Super in java methods
- 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