Java Interview Questions Set 10
Categories: Java 8(JDK1.8)
What is this keyword in java?
The this keyword is a reference variable that refers to the current object. There are the various uses of this keyword in Java. It can be used to refer to current class properties such as instance methods, variable, constructors, etc. It can also be passed as an argument into the methods or constructors. It can also be returned from the method as the current class instance.
What is composition?
Holding the reference of a class within some other class is known as composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition. In other words, we can say that composition is the particular case of aggregation which represents a stronger relationship between two objects. Example: A class contains students. A student cannot exist without a class. There exists composition between class and students.
What is the difference between aggregation and composition?
Aggregation represents the weak relationship whereas composition represents the strong relationship. For example, the bike has an indicator (aggregation), but the bike has an engine (composition).
Why does Java not support pointers?
The pointer is a variable that refers to the memory address. They are not used in Java because they are unsafe(unsecured) and complex to understand.
What is super in java?
The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. Whenever you create the instance of the subclass, an instance of the parent class is created implicitly which is referred by super reference variable. The super() is called in the class constructor implicitly by the compiler if there is no super or this.
What are the main uses of the super keyword?
There are the following uses of super keyword.
a) super can be used to refer to the immediate parent class instance variable.
b) super can be used to invoke the immediate parent class method.
c) super() can be used to invoke immediate parent class constructor.
What are the differences between this and super keyword?
There are the following differences between this and super keyword.
a) The super keyword always points to the parent class contexts whereas this keyword always points to the current class context.
b) The super keyword is primarily used for initializing the base class variables within the derived class constructor whereas this keyword primarily used to differentiate between local and instance variables when passed in the class constructor.
c) The super and this must be the first statement inside constructor otherwise the compiler will throw an error.
What is the output of the following Java program?
class Person
{
public Person()
{
System.out.println("Person class constructor called");
}
}
public class Employee extends Person
{
public Employee()
{
System.out.println("Employee class constructor called");
}
public static void main (String args[])
{
Employee e = new Employee();
}
}
Output
Person class constructor called
Employee class constructor called
Can you use this() and super() both in a constructor?
No, because this() and super() must be the first statement in the class constructor.
Example:
public class Test{
Test()
{
super();
this();
System.out.println("Test class object is created");
}
public static void main(String []args){
Test t = new Test();
}
}
Output:
Test.java:5: error: call to this must be first statement in constructor