Core Java - OOPs Concepts: static keyword Interview Questions
1.What if the static modifier is removed from the signature of the main method?
Program compiles. However, at runtime, It throws an error “NoSuchMethodError.”
2. What is the difference between static (class) method and instance method?
static or class method | instance method |
---|---|
1)A method that is declared as static is known as the static method. | A method that is not declared as static is known as the instance method. |
2)We don’t need to create the objects to call the static methods. | The object is required to call the instance methods. |
3)Non-static (instance) members cannot be accessed in the static context (static method, static block, and static nested class) directly. | Static and non-static variables both can be accessed in instance methods. |
4)For example: public static int cube(int n){ return nnn;} | For example: public void msg(){…}. |
3.Can we make constructors static?
- the static context (method, block, or variable) belongs to the class, not the object.
- Since Constructors are invoked only when the object is created, there is no sense to make the constructors static.
- if you try to do so, the compiler will show the compiler error.
4.Can we make the abstract methods static in Java?
- In Java, if we make the abstract methods static, It will become the part of the class, and we can directly call it which is unnecessary.
- Calling an undefined method is completely useless therefore it is not allowed.
5.Can we declare the static variables and methods in an abstract class?
- Yes, we can declare static variables and methods in an abstract method.
- there is no requirement to make the object to access the static context
- we can access the static context declared inside the abstract class by using the name of the abstract class.
abstract class Test
{
static int i = 102;
static void TestMethod()
{
System.out.println("hi !! I am good !!");
}
}
public class TestClass extends Test
{
public static void main (String args[])
{
Test.TestMethod();
System.out.println("i = "+Test.i);
}
}
hi !! I am good !!
i = 102
Core Java - OOPs Concepts: Inheritance Interview Questions
6.What is this keyword in java?
- this is a reference variable that refers to the current object.
- 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.
7.What are the main uses of this keyword?
There are the following uses of this keyword.
- this can be used to refer to the current class instance variable.
- this can be used to invoke current class method (implicitly)
- this() can be used to invoke the current class constructor.
- this can be passed as an argument in the method call.
- this can be passed as an argument in the constructor call.
- this can be used to return the current class instance from the method.
1.this can be used to refer to the current class instance variable.
The this keyword can be used to refer current class instance variable.
If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.
without this keyword
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
rollno=rollno;
name=name;
fee=fee;
}
void display(){
System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis1{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}
}
0 null 0.0
0 null 0.0
In the above example, parameters (formal arguments) and instance variables are same. So, we are using this keyword to distinguish local variable and instance variable.
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}
}
111 ankit 5000.0
112 sumit 6000.0
If local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program:
class Student{
int rollno;
String name;
float fee;
Student(int r,String n,float f){
rollno=r;
name=n;
fee=f;
}
void display(){System.out.println(rollno+" "+name+" "+fee);
}
}
class TestThis3{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}
}
111 ankit 5000.0
112 sumit 6000.0
2.this can be used to invoke current class method (implicitly)
The this keyword can be used to refer current class instance variable.
If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.
public class Test2 {
public static void main(String args[]) {
Test2 a = new Test2();
a.n();
}
void m(){System.out.println("hello a");}
void n(){
System.out.println("hello b");
//m(); or use this.but compiler automatically adds this keyword
this.m();
}
}
hello b
hello a
3. this() can be used to invoke the current class constructor.
The this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor. In other words, it is used for constructor chaining.
Calling default constructor from parameterized constructor:
public class Test2 {
Test2(){
System.out.println("hello a");
}
Test2(int x){
this();
System.out.println(x);
}
public static void main(String args[]){
Test2 a= new Test2(5);
}
}
hello a
5
Calling parameterized constructor from default constructor:
public class Test2 {
Test2(){
this(5);
System.out.println("hello a");
}
Test2(int x){
System.out.println(x);
}
public static void main(String args[]){
Test2 a= new Test2();
}
}
5
hello a
Real usage of this() constructor call
- The this() constructor call should be used to reuse the constructor from the constructor.
- It maintains the chain between the constructors i.e. it is used for constructor chaining.
public class Student {
int rollno;
String name,course;
float fee;
Student(int rollno,String name,String course){
this.rollno=rollno;
this.name=name;
this.course=course;
}
Student(int rollno,String name,String course,float fee){
this(rollno,name,course);//reusing constructor
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);}
public static void main(String args[]){
Student s1=new Student(111,"ankit","java");
Student s2=new Student(112,"sumit","java",6000f);
s1.display();
s2.display();
}
}
111 ankit java 0.0
112 sumit java 6000.0
📢 Call to this() must be the first statement in constructor.
4. this can be passed as an argument in the method call.
The this keyword can also be passed as an argument in the method. It is mainly used in the event handling.
Application of this that can be passed as an argument:
In event handling (or) in a situation where we have to provide reference of a class to another one. It is used to reuse one object in many methods.
public class Student {
void method1(Student student){
System.out.println("method is invoked");
}
void method2(){
method1(this);
}
public static void main(String args[]){
Student s1 = new Student();
s1.method2();
}
}
method is invoked
5. this can be passed as an argument in the constructor call.
We can pass the this keyword in the constructor also. It is useful if we have to use one object in multiple classes.
// Java code for using this as an argument in constructor
// call
// Class with object of Class B as its data member
class A
{
B obj;
// Parameterized constructor with object of B
// as a parameter
A(B obj)
{
this.obj = obj;
// calling display method of class B
obj.display();
}
}
class B
{
int x = 5;
// Default Constructor that create a object of A
// with passing this as an argument in the
// constructor
B()
{
A obj = new A(this);
}
// method to show value of x
void display()
{
System.out.println("Value of x in Class B : " + x);
}
public static void main(String[] args) {
B obj = new B();
}
}
Value of x in Class B : 5
6. this can be used to return the current class instance from the method.
We can return this keyword as an statement from the method. In such case, return type of the method must be the class type (non-primitive)
public class Student {
Student getStudent(){
return this;
}
void method(){
System.out.println("hello");
}
public static void main(String[] args) {
new Student().getStudent().method();
}
}
hello
Proving this keyword
we prove that this keyword refers to the current class instance variable. In this program, we are printing the reference variable and this, output of both variables are same.
public class Student {
void method(){
System.out.println(this);
}
public static void main(String[] args) {
Student student = new Student();
System.out.println(student);
student.method();
}
}
gengic.Student@1b6d3586
gengic.Student@1b6d3586
8.Can this keyword be used to refer static members?
- Yes, this keyword is used to refer static members because this is just a reference variable refers to the current class object.
- it is unnecessary to access static variables through objects, therefore, it is not the best practice to use this to refer static members.
public class Test
{
static int i = 10;
public Test ()
{
System.out.println(this.i);
}
public static void main (String args[])
{
Test t = new Test();
}
}
10
9. What are the advantages of passing this into a method instead of the current class object itself?
- this refers to the current class object, it must be similar to the current class object.
- -there can be two main advantages of passing this into a method instead of the current class object.
– this is a final variable. Therefore, this cannot be assigned to any new value whereas the current class object might not be final and can be changed.
– this can be used in the synchronized block.
10.What is the Inheritance?
- Inheritance is a mechanism by which one object acquires all the properties and behavior of another object of another class.
- It is used for Code Reusability and Method Overriding. The idea behind inheritance in Java is that you can create new classes that are built upon existing classes.
- When you inherit from an existing class, you can reuse methods and fields of the parent class.
- Moreover, you can add new methods and fields in your current class also.
- Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
There are five types of inheritance in Java.
Single-level inheritance
Multi-level inheritance
Multiple Inheritance
Hierarchical Inheritance
Hybrid Inheritance