OCAJP – How to use these and other super keywords in Java?

OCAJP – How to use these and other super keywords in Java?

April 21, 2023 Off By Evelyn

We are currently writing the series on OCAJP certification exam. We offer OCAJP 7, OCAJP8, OCPJP 6 and OCPJP 7, OCPJP 7 for programmer-level certifications. Keep checking our blog for interesting tips on certification topics.
We also wrote about how to prepare to take the OCAJP exam and how to prepare to take the OCPJP certification exam. These posts can be very helpful if you’re looking for guidance in preparing for certification exams.

This post is about the OCAJP exam objective: “Use super to access objects and constructors”. You will be tested on super,this keyword usage during the exam. We will attempt to cover all aspects of this exam objective. We will show you how to use this keyword and super in the following sections.
What is the purpose of this keyword in Java
The keyword “this” is used to refer to the curreynt instance or object. This keyword refers to the current instance or context of the class. This makes it easier to create an object by using the “new keyword”.
To access present class object instance variables, the keyword “this” is used.
“this” is used most often in constructors when initializing instance variables, if constructor parameters names are the same as instance variables.
The keyword “this” can be used to access the instance variables.
Example:
public class Test int num1;int num2;public Test(int num1, int num2) this.num1 = num1;this.num2 = num2;public static void main(String[] args) Test t = new Test(7, 8);System.out.println(t.num1 + ” ” + t.num2);//it prints 7 8If constructor parameter names and instance variables are different, you are not required to use this. This will prevent any compile-time errors.
Example:
public class Test int num1;int num2;public Test(int n1, int n2) num1 = n1;num2 = n2;public static void main(String[] args) Test t = new Test(7, 8);System.out.println(t.num1 + ” ” + t.num2);// it prints 7 8When super class and sub class contains same variable names,sub class variable hides the super class variable . It is called variable hiding.
If you print this variable, JVM will always print sub-class variable value
Take a look at the following example to grasp this point
Example:
public class Test extends Test1 int oca = 8;public void printValue() System.out.println(oca);//prints 8public static void main(String[] args) Test t = new Test();t.printValue();class Test1 int oca = 7;In the above situation, you should use “this” to call present class current object instance variable and super to call super class instance variable
Example:
public class Test extends Test1 int oca = 8;public void printValue() System.out.println(this.oca + ” ” + super.oca);// prints 8 7public static void main(String[] args) Test t = new Test();t.printValue();class Test1 int oca = 7;You can also use “this” to call instance method from another instance method
Example:
public class Test int oca = 8;public void printValue() System.out.println(this.oca);// prints 8public void call() this.printValue();public static void main(String[] args) Test t = new Test();t.call();You can use super to call parent class method when that method is overridden in sub class
Example:
public class Test extends Test1 int oca = 8;public int giveValue() return oca;public void call() System.out.println(giveValue());// prints 8System.out.println(super.giveValue());// prints 7public static void main(String[] args) Test t = new Test();t.call();class Test1 int oca = 7;public int giveValue() return oca;You can’t use both this, super in static method. If you use you get compile time error
Example:
public class Test extends Test1 int oca = 8;public int giveValue() return oca;public static void call() System.out.println(this.giveValue());// generates compile time// error,this can’t be used in// static methodSystem.out.println(super.giveValue());// generates compile time// error,super can’t be used in// static methodpublic static void main(String[] args) Test t = new Test();t.call();class Test1 int oca = 7;public int giveValue() return oca;What is the use of this()?
This() can be used to call constructor to constructor for the same class
You must pass the appropriate arguments to this() when you call constructor with this()
Take a look at the following example to see how we can understand this point.
Example:
public class Test int num1;int num2;Test() this(7);// line 1Test(int num1) this(num1, 8);// line 2Test(int num1, int num2) this.num1 = num1;this.num2 = num2;public static void main(String[] args) Test t = new Test();System.out.println(t.num1 + ” ” + t.num2);At line 1 , you are calling one argument constructor. You must pass one argument
Line 2: You are calling two argument constructors. You must pass two arguments
In