Top Java Interview Questions and Answers Intermediate Level
This is our second set in the Java Interview Questions series. If you want to check out the first set then you can check it out here.
In this interview question series, we will look into advanced Java interview questions about advanced topics like Inheritance, Types of Inheritance, Constructors, class and object, Aggregation, and Composition.

Top Java Interview Questions and Answers Intermediate Level
1. What are the restrictions that are applied to the Java static methods?
There are two restrictions that are applied to the static methods, they are:
- The static method can only use and call static data members or methods
- Keywords like this and super cannot be used as they are non-static.
2. Why is the main method static?
There are plenty of reasons why the main method is static. One of the most important reasons is if we make the main method non-static, JVM will create its object and then call the main() method which will need extra memory allocation and it makes it easier to understand and read, which will improve the efficiency of developers.
The purpose of a static main method is to provide default or fixed values for parameters such as input or output files, input or output messages, and so on. It also simplifies code development by providing data-type checking and compile-time type checking.
3. Can we override the static methods?
No, static methods can't be overridden because method overriding is based on dynamic binding. Since static methods are bonded using static binding at compile time, we can't override the static method.
4. What is the static block?
In Java programming language, to initialize the static data member the static block is used and the static keyword is used to create a static block. It is executed before the main method at the time of class loading. A static block can contain executable code, but it cannot refer to instance variables of an object in the same way as a non-static block can. Learn More.
5. Can we execute a program without main() method?
We can't execute a program without a main() method. it was possible with JDK 1.7. Learn more.
6. What if the static modifier is removed from the signature of the main method?
If you removed the static modifier from the signature of the main method then the compile will compile the program but will throw an error "NoSuchMethodError."
7. Can you make constructors static?
You can't make a constructor static, if you try to make it static compiler will throw an error. Static methods, blocks, and variables belong to the class and not the object. Constructors are called only when the object is created, it doesn't make any sense to make a constructor static.
8. Can you make the abstract methods static in Java?
The abstract methods can't be static because when you declare a method in the abstract class, you need to override it in the subclass. Since overriding is not possible with static methods, we can't make an abstract method as static. If you still try to do that, the abstract method will become part of the class, and calling an undefined method is useless if it is not allowed.
9. Can you declare the static variables and methods in an abstract class?
Yes, you can declare static variables and methods in an abstract class because the object can access the static content without any requirement and you can access the static content by using the name of the abstract class. Look at the following example-
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
10. What is this keyword in java?
This keyword refers to the current object. The this keyword is used to prevent confusion when you have the same variable names for local variables and instances. It can also be passed as an argument into the methods and constructors. Learn More.
Example of this keyword-
class Demo
{
Double width, height, depth;
Demo (double w, double h, double d)
{
// Syntax of this keyword
this.width = w;
this.height = h;
this.depth = d;
}
public static void main(String[] args) {
Demo d = new Demo(10,20,30);
System.out.println("width = "+d.width);
System.out.println("height = "+d.height);
System.out.println("depth = "+d.depth);
}
}
width = 10.0
height = 20.0
depth = 30.0
11. What is the purpose of this keyword in Java?
This keyword is used for different purposes, but the most common of them are-.
- this keyword is used to refer to the current object.
- this is always a reference to the object on which the method was invoked.
- this can be used to invoke the current class constructor.
- this can be passed as an argument to another method.
12. What is the Output of following code?
class Demo
{
Double width, height, depth;
Demo (double w, double h, double d)
{
this.width = w;
this.height = h;
this.depth = d;
}
public static void main(String[] args) {
Demo d = new Demo(10,20,30);
System.out.println("width = "+d.width);
System.out.println("height = "+d.height);
System.out.println("depth = "+d.depth);
}
}
width = 10.0
height = 20.0
depth = 30.0
13. Can you assign the reference to this variable?
No, you can't assign any value because it always refers to the current class object. If you still try to assign a value, the compiler will throw an error. Look at the following example-
public class Test
{
public Test()
{
this = null;
System.out.println("Test class constructor called");
}
public static void main (String args[])
{
Test t = new Test();
}
}
Test.java:5: error: cannot assign a value to final variable this
this = null;
^
1 error
14. Can this keyword be used to refer to static members?
Yes, you can use this keyword to refer to static members. The this keyword just refers to the current class object and it is unnecessary to use this keyword for accessing static variables. It's not a best practice to use this keyword to refer static members. Look at the following example-
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
15. What is Inheritance?
Inheritance is one of the most important features in Java programming language. It enables the programmer to use a class and extends it to form a new class.
The programmer can modify methods, add new functions, and override the existing ones. The inherited objects get the features of their parent classes while also having their own unique characteristics.
The main purpose of java inheritance is to make changes in a code easier and less time-consuming. Learn More.
16. What are the Types of inheritance in Java?
Multiple Inheritance is not supported in Java like in other programming languages. These are the following types of inheritance in Java-
- Single-level inheritance
- Multi-level inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
- Learn more about these types here.
17. What is the purpose of Inheritance?
- Inheritance allows us to reuse the code.
- It promotes polymorphism by allowing method overriding.
- It provides data hiding too that can be used to hide data from derived classes by making the base class private.
18. Which class is the superclass for all the classes?
The object class is the superclass and root for all the other classes because all the predefined and user-defined classes present are subclasses of the Object class.
19. Why is multiple inheritance not supported in java?
Java supports single inheritance only. A class can only extend one base class other than the Object class. This restriction is in place to help avoid common pitfalls encountered in languages that support multiple inheritance. Multiple inheritances imply that a child inherits aspects of its parent classes and implements the same kind of behavior as those parent classes separately.
This might lead to problems because it is difficult to know what exactly has been inherited, and if two parents have implemented a certain feature differently, confusion might arise as to which implementation is correct for the child.
20. What is aggregation?
Aggregation works as a one-way relationship between two objects in Java. Aggregation can be described as a has-a relationship. For example, if we created a student class it contains fields like age, name, id, and school and also contains an object having the address of that student. Now, We can say that the Student class has a reference to the Address class but the Address class can't have a reference to the Student class.
Class Address{
int street_no;
String city;
String state;
int pin;
Address(int street_no, String city, String state, int pin ){
this.street_no = street_no;
this.city = city;
this.state = state;
this.pin = pin;
}
}
class Student
{
String name;
Address ad;
}
More details.
21. What is composition?
a Composition is a form of Aggregation that has more restrictions than Aggregation. Composition is the holding of a class reference within another class. It is referred to as composition when one item contains another and the contained object cannot exist without the container object.
To put it another way, we may say that composition is a specific instance of aggregation, which signifies a greater bond between two objects. Example: There are students in a class. Without a class, a student would not exist. The composition of the class and the students is present.
22. What is the difference between aggregation and composition?
The only difference that aggregation and composition has is, Composition represents a strong relationship and Aggregation has a weak relationship. For example, a student has an address(Aggregation) but the student has a home(Composition).
23. Why does Java not support pointers?
A pointer is a variable that stores the address of another variable or memory location. In Java programming language, there is no direct support for pointers and all variables are stored on the stack. This means that you cannot access memory directly instead, you have to use certain operations to read and write from it.
This feature does not exist in Java as it can cause some major problems with concurrent programming which is a popular design pattern in Java. Pointers are also not needed because most of the work done using pointers can be done using various array operations and other features of the language.
24. What is super in java?
The super keyword in Java is used when calling a constructor from a subclass. The purpose of this keyword is to create an instance of the superclass and pass it to any required arguments. You can also use this keyword in event handler methods, constructors, or as a method returning type, but it will behave differently depending on where you use it.
class Animal{
Animal(){System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
System.out.println("dog is created");
}
}
class TestSuper4{
public static void main(String args[]){
Dog d=new Dog();
}
}
animal is created
dog is created
25. How can constructor chaining be done by using the super keyword?
class Person
{
String name,address;
int age;
public Person(int age, String name, String address)
{
this.age = age;
this.name = name;
this.address = address;
}
}
class Employee extends Person
{
float salary;
public Employee(int age, String name, String address, float salary)
{
super(age,name,address);
this.salary = salary;
}
}
public class Test
{
public static void main (String args[])
{
Employee e = new Employee(22, "Mukesh", "Delhi", 90000);
System.out.println("Name: "+e.name+" Salary: "+e.salary+" Age: "+e.age+" Address: "+e.address);
}
}
Name: Mukesh Salary: 90000.0 Age: 22 Address: Delhi
26. What are the main uses of the super keyword?
The following are the main uses of the super keyword-
- The super keyword can be used to refer to the parent class instance variable.
- You can call the parent class method using super.
- the super() function can invoke the parent class constructor.
27. 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();
}
}
Person class constructor called
Employee class constructor called
28. Can you use this() and super() both in a constructor?
No, you use both this() and super() in a constructor. You can't use them in the first line of code as they can't be executed at once.
public class Test{
Test()
{
super();
this();
System.out.println("Test class object is created");
}
public static void main(String []args){
Test t = new Test();
}
}
Test.java:5: error: call to this must be the first statement in the constructor
29. What is object cloning?
Cloning objects is one way to create multiple copies of an object’s state without having to use the same resources and memory. It is used to create the exact copy of an object that has the same properties. If you want to make a copy of an object, you can use clone() method to clone the object.
Example-
class Student18 implements Cloneable{
int rollno;
String name;
Student18(int rollno,String name){
this.rollno=rollno;
this.name=name;
}
public Object clone()throws CloneNotSupportedException{
return super.clone();
}
public static void main(String args[]){
try{
Student18 s1=new Student18(101,"avdhoot");
Student18 s2=(Student18)s1.clone();
System.out.println(s1.rollno+" "+s1.name);
System.out.println(s2.rollno+" "+s2.name);
}catch(CloneNotSupportedException c){}
}
}
101 avdhoot
101 avdhoot
30. What is method overloading?
Method overloading is a way of having more than one method with the same name in a class, but with different arguments. It allows programmers to create different versions of methods that behave differently, depending on the data supplied as input.
The main benefit of method overloading is that it allows programmers to create multiple versions of methods, reducing code duplication and making code easier to understand.
It also reduces the number of names that need to be remembered. The drawback is that some people find it difficult to understand which version will be invoked depending on where they are looking within the codebase.
Example-
class Calculate
{
void sum (int a, int b)
{
System.out.println("sum is"+(a+b)) ;
}
void sum (float a, float b)
{
System.out.println("sum is"+(a+b));
}
Public static void main (String[] args)
{
Calculate cal = new Calculate();
cal.sum (8,5); //sum(int a, int b) is method is called.
cal.sum (4.6f, 3.8f); //sum(float a, float b) is called.
}
}
Sum is 13
Sum is 8.4