Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming /Difference Between this and super in Java

Difference Between this and super in Java

January 30, 2018Posted byLithmee

Key Difference – thisvs super in Java

The keywords ‘this’ and ‘super’ are used inJavaprogramming. These keywords cannot be used as variables or any other identifier name. Java supportsObject Oriented Programming(OOP). The program or software can be modeled usingobjects. Objects are insatiate usingclasses. One pillar of OOP isinheritance. It provides code reusability. The classes that already exist aresuperclasses, and the derived classes aresubclasses. The super keyword can be used to refer an object of the superclass. There are multiple objects in the system. The ‘this’ keyword is used to refer a current object. Thekey differencebetween this and super is ‘这是一个引用变量,用于refer the current objectwhile ‘super’ is a reference variable that is used to refer immediate superclass object.

CONTENTS

1.Overview and Key Difference
2.What is this in Java
3.What is super in Java
4.Similarities Between this and super in Java
5.Side by Side Comparison – this vs super in Java in Tabular Form
6.Summary

What is this in Java?

The keyword ‘this’ is used to refer a current object. Refer the given Java program.

Difference Between this and super in Java

Figure 01: Java program using this keyword

In Java, there are three types of variables. They areinstance variables, local variablesand class variables. According to the above program, class Employee has two instance variables. They are id and name. Local variables are the variables belongs to methods. Class variables are shared by all objects. The id and name are passed to the Employee constructor. If the programmer writes id = id; it won’t initialize the instance variables because the Constructor already has id and name. There are no values for instance variables. So, printing them will display null. When using this, it refers to the current object. Therefore, giving id and name to the constructor can set the instance variables.

The keyword ‘this’ can be used to invoke the current class method. Refer to the given java program.

public class ThisDemo{

public static void main(String[] args){

Myclass myClass= new Myclass();

myClass.B();

}

}

class Myclass{

public void A(){

System.out.println(“A”);

}

public void B(){

System.out.prinltn(“B”);

this.A();

}

}

The class Myclass contains two methods. They are method A and B. When creating an object of Myclass and invoking the method B will print B, A as the output. In method B, after printing B there is a statement as this.A(). Using this, the current class method was invoked.

It is also possible to use this keyword to invoke the current class constructor. Refer the given program.

public class ThisDemo{

public static void main(String[] args){

A obj = new A(5);

}

}

class A{

public A(){

System.out.println(“Constructor A”);

}

public A(int x){

this();

System.out.println(“Parameterized Constructor A”);

}

}

According to the above program, class A has a default constructor and a parameterized constructor. When creating an object of A, the parameterized constructor is called. In the parameterized constructor, there is a statement like this(); It will call the current class constructor that is A().

What is super in Java?

The keyword ‘super’ is related to inheritance. Inheritance is a major concept of Object Oriented Programming. It allows using the properties and methods of the already existing class to a new class. The already existing class is known as the parent class or superclass. The new class is known as the child class or subclass.

The ‘super’ is a reference variable that is used to refer the immediate parent class object. The super keyword can refer immediate parent class instance variable or invoke immediate parent class method. The super() is used to invoke immediate parent class constructor.

假设有两类A and B. Class A is the superclass and class B is the subclass. Class A, B both have display method.

public class A{

public void display(){

System.out.println(“A”);

}

}

public class B extends A{

public void display(){

System.out.println(“B”);

}

}

When creating an object of type B and calling the method display, it will give the output B. class A has the display method, but it is overridden by the subclass B display method. If the programmer wants to call the display method in class A, then he can use the super keyword. Refer the given Java program.

Difference Between this and super in Java_Figure 02

Figure 02: Java program using super keyword

According to the above program, class A has a variable named number with value 10. Class B extends A and has a variable named number with value 20. Generally, when creating an object of type B and calling the display method should give the number in the subclass because the superclass value is overridden by the new class. By using the super.num, the superclass number value is printed.

The super() can be used to call the superclass constructor. Refer the below program.

public class Main {

public static void main(String[] args){

B obj = new B();

}

}

class A{

A(){

System.out.println(“A”);

}

}

class B extends A{

B(){

super();

System.out.println(“B”);

}

}

According to the above program, class A has a constructor A (). Class B has the constructor B (). Class B extends class A. When creating an object of type B, it will print A, B as the output. The B () constructor has super (). Therefore, first the A constructor is invoked and then goes to B. Even though super () is not written, by default the parent constructor is called.

The super using the method is as follows.

Key Difference Between this and super in Java

Figure 03: Java program that invokes the superclass method

According to the above program, class A has display method. Class B also has display method. Class B extends A. When creating an object of type B and calling the display method will give output as A and B. In class B display method, class A display method is called using super.display(). That method prints “A” first. Then prints “B”.

What is the Similarity Between this and super?

  • Both are keywords in Java programming.

What is the Difference Between this and super?

This vs Super

The ‘this’ is a reference variable that is used to refer the current object. The ‘super’ is a reference variable that is used to refer an immediate superclass object.
Instance Variable
A current class instance variable can be referred using this. Superclass instance variable can be referred using super.
Class Method
The current class method can be invoked using this. Superclass method can be invoked using super.
Constructor
The current class constructor can be invoked using this(). Superclass constructor can be invoked using super().

Summary – thisvs super in Java

The keywords ‘this’ and ‘super’ are used in Java. The keywords cannot be used as variables or any other identifier name. They seem to be the same, but they have a difference. The difference between this and super is that super is a reference variable that is used to refer immediate superclass object while this is a reference variable that refers the current object.

Download the PDF of this vs super in Java

You can download PDF version of this article and use it for offline purposes as per citation note. Please download the PDF version here:Difference Between this and super in Java

Reference:

1.This() and super keyword in java and its purpose with a short example, Atoz knowledge, 31 Jan. 2015.Available here
2.“This keyword in java – java point.”Available here
3.tutorialspoint.com. “Java Inheritance.”The Point,Available here

Related posts:

Difference Between Logical Address and Physical AddressDifference Between Logical Address and Physical Address Difference Between Assembler and Compiler Difference Between Inheritance and Composition Difference Between Git and GithubDifference Between Git and Github Difference Between Runnable and Thread_Figure 03Difference Between Runnable and Thread

Filed Under:ProgrammingTagged With:Compare this and super in Java,在Java中超级,在Java中超级Class,在Java中超级Constructor,在Java中超级Definition,在Java中超级Variable,this and super in Java Differences,this and super in Java Similarities,this in Java,this in Java Class,this in Java Constructor,this in Java Definition,this in Java Variable,this vs super in Java

About the Author:Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a ReplyCancel reply

Your email address will not be published.Required fields are marked*

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Acropetal and Basipetal Order

Difference Between Acropetal and Basipetal Order

Difference Between Smartphone and Tablet and Laptop

Difference Between CMS and Framework

Difference Between CMS and Framework

Difference Between Conventional and Traditional

Difference Between Conventional and Traditional

Difference Between Torque and Horsepower

Latest Posts

  • What is the Difference Between Care and Concern
  • What is the Difference Between Ehlers Danlos and Marfan Syndrome
  • What is the Difference Between Solubilizer and Emulsifier
  • What is the Difference Between Sanitation and Sterilization
  • What is the Difference Between Gaviscon Advance and Gaviscon Double Action
  • What is the Difference Between Saponin and Sapogenin
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018Difference Between. All rights reserved.Terms of Useand Privacy Policy:Legal.