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();
- }
- }
Output
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();
- }
- }
Output
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();
- }
- }
Output
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();
- }
- }
Output
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();
- }
- }
Output
Now see the difference when the this() keyword is used. This keyword is used to differentiate between a local variable and an instance variable.
- class Children
- {
- int stid;
- String stname;
- Children(int stid, String stname)
- {
- this.stid = stid;
- this.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();
- }
- }
Output
2. Using the this() keyword invokes the current class method.
If this keyword is not used in a program then the compiler automatically adds this keyword when using (invoking) the method.
- class InvokeMethodEx
- {
- void main()
- {
- System.out.println("used this() our method invoked");
- }
- void sub()
- {
- this.main();
- }
- void sub1()
- {
- sub();
- }
- public static void main(String args[])
- {
- InvokeMethodEx im1 = new InvokeMethodEx();
- im1.sub1();
- }
- }
Output
3. Using the this() keyword to pass an argument in the constructor call.
It is useful if we use one object in many classes, as in:
- class Parent
- {
- Children obj;
- Parent(Children obj)
- {
- this.obj = obj;
- }
- void print()
- {
- System.out.println(obj.data);
- }
- }
-
- class Children
- {
- int data = 10000;
- Children()
- {
- Parent b1 = new Parent(this);
- b1.print();
- }
- public static void main(String args[])
- {
- Children a = new Children();
- }
- }
Output
4. Using the this() keyword to invoke the current class constructor.
For reusing constructors the this() keyword is a better approach.
- class Children
- {
- int stid;
- String stname;
- Children()
- {
- System.out.println("default constructor is invoked");
- }
- Children(int stid, String stname)
- {
- this();
- this.stid = stid;
- this.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();
- }
- }
Output
5. Using the this() keyword to pass an argument in the method call.
We can pass an argument to our method. It is used in event handling, as in:
- class InvokeMethodEx
- {
- void main(InvokeMethodEx obj)
- {
- System.out.println("used this() our method invoked");
- }
- void sub()
- {
- main(this);
- }
- public static void main(String args[])
- {
- InvokeMethodEx im1 = new InvokeMethodEx();
- im1.sub();
- }
- }
Output
6. Using the this() keyword to return the current class instance
We can return the this keyword as a statement from the methods.
Syntax for this:
- return_type_method_name()
- {
- return this;
- }
Summary
In this article, we learned about super and this keywords of Java programming language and how to use them in Java program.