Introduction
In this article, we discuss "super and this keyword in Java". super(), as well as this() both, are used to make constructor calls. super() is used to call Base class’s constructor(i.e, Parent’s class) while this() is used to call current class’s constructor.
Super-Keyword
Keywords are reserved words in Java, that have a predefined meaning to the compiler. The Super keyword indicates the following:
- In our program when methods are overridden, then super is used to access the super-class methods, constructors and variables.
- The super keyword in the Java programming language refers to the superclass of the class where the super keyword is currently being used.
- Super is also used for a hidden field.
- With the use of the keyword super, we can access a super-class member.
Super() is used to invoke:
- parent class instance variable.
- parent class constructor.
- parent class methods.
1. Refer to the parent class variable through super()
Example
Without the use of the super() keyword
- class Car
- {
- int avgspeed = 250;
- }
- class Bus extends Car
- {
- int avgspeed=320;
- void print()
- {
- System.out.println(avgspeed);
- }
- public static void main(String args[])
- {
- Bus b = new Bus();
- b.print();
- }
- }

Now see the difference with the use of the super() keyword
- class Car
- {
- int avgspeed = 250;
- }
- class Bus extends Car
- {
- int avgspeed=320;
- void print()
- {
- System.out.println(super.avgspeed);
- }
- public static void main(String args[])
- {
- Bus b = new Bus();
- b.print();
- }
- }

2. Invoke parent class constructor through super()
We can invoke or call a constructor from the parent class as in:
- class Car
- {
- Car()
- {
- System.out.println("Car is launched");
- }
- }
- class Bus extends Car
- {
- Bus()
- {
- super();
- System.out.println("Bus is lauched");
- }
- public static void main(String args[])
- {
- Bus b = new Bus();
- }
- }

3. Invoke parent class method through super()
We can invoke a parent class method through the use of super() as in:
- class Parent
- {
- void information()
- {
- System.out.println("Hello...!");
- }
- }
- class Children extends Parent
- {
- void information()
- {
- System.out.println("Hello world......!");
- }
- void print()
- {
- information();
- super.information();
- }
- public static void main(String args[])
- {
- Children c = new Children();
- c.print();
- }
- }

This-keyword in Java
In Java Programming "this" is a reference keyword used to refer to the current class method, contructor, etc.
How this() Keyword in Java is used?
1. Used to refer to the current class variable
Without the use of this() keyword
- class Children
- {
- int stid;
- String stname;
- Children(int stid, String stname)
- {
- stid = stid;
- stname = stname;
- }
- void print()
- {
- System.out.println(stid + " " + stname);
- }
- public static void main(String args[])
- {
- Children st1 = new Children(12, "John");
- Children st2 = new Children(22, "Sam");
- st1.print();
- st2.print();
- }
- }






Join the conversation! Your thoughts help the community grow.