Java Interview questions and answers
Java Interview questions and answers
The process of binding data and corresponding methods (behavior) together into a
single unit is called encapsulation.
3. How to achieve or implement encapsulation in Java?
There are two points thereby we can achieve or implement encapsulation in Java
program.
1. Declaring instance variable of class as private so that it cannot be accessed directly by
anyone from outside the class.
2. Provide the public setter and getter methods in the class to set/modify the value of variable.
4. What is Data hiding in Java? How to achieve it programmatically?
If each variable is declared as private in the class, it is called tightly encapsulated class
in Java.
6. What would happen if we do not use Encapsulation in a program?
If we don’t use encapsulation in a program, fields will not be private and could be
accessed by anyone from outside the class.
7. What is getter and setter method in Java?
A method that is used to retrieve/get the value of a variable or return the value of the
private member variable is called getter method.
A method that is used for updating or setting the value of a variable is called setter method.
8. Explain inheritance.
The technique of creating a new class by using an existing class functionality is called
inheritance. In other words, inheritance is a process where a child class acquires all the
properties and behaviors of parent class. The existing class is called parent class and the new
class is called child class.
9. What is Is-A relationship in Java?
Is-A relationship represents inheritance. It is implemented using “extends” keyword. It
is used for code reusability. All the classes extends java.lang.object by default. This is a very
good example of Is-A relationship.
10. Which class in Java is super class of every other class?
Multiple inheritance means that one class extends two superclasses or base classes but
in Java, one class cannot extend more than one class simultaneously. At most, one class can
extend only one class.
Therefore, to reduce ambiguity, complexity, and confusion, Java does not support multiple
inheritance through class.
For more detail, go to this tutorial: Types of Inheritance in Java
15. How does multiple inheritance implement in Java?
Multiple inheritance can be implemented in Java by using the interface. A class cannot
extend more than one class but a class can implement more than one interface.
16. What are the uses of super keyword in Java?
this keyword is a reference variable that refers to the current class object. It holds the
reference to current class object or same class object.
There are six usages of Java this keyword. They are as follows:
1. this reference can be used to refer to the current class instance variable.
2. this keyword is used to call the non-static method of the current class.
3. this() can be used to invoke the current class constructor.
4. this keyword can be used as a parameter in the method call.
5. The keyword “this” can be used as a parameter in the constructor call.
6. It can also be used to return the object of the current class from the method.
18. Is it possible to use this() and super() both in same constructor?
No, Java does not allow using both super() and this() together in same constructor. As
per Java specification, super() or this() must be the first statement in a constructor.
19. What are the differences between super and this keyword?
When a class has more than one method having the same name but different in
parameters, it is called method overloading in Java.
21. How method overloading is implemented in Java?
Here is the list of rules by which method overloading can be implemented in Java. They
are:
i. The method name must be the same.
ii. Parameters must be different as
a. Data types of parameters
b. Number of parameters
c. Sequence of data type of parameters
iii. Access specifiers can be anything or different.
iv. Return type can be anything or different.
v. Exception thrown can be anything.
22. What are the features of method overloading?
24. Why method overloading is not possible by changing return type of method?
In Java, Method overloading cannot be done when the return type, method name, and
argument list are the same because there may occur ambiguity.
Yes, we can overload the main() method in Java. A class can have any number of
main() methods but the execution always starts from public static void main(String[] args)
only.
The process of converting a value from one data type to another is known as type
conversion.
When two data types are compatible with each other, Java compiler performs the
conversion automatically or implicitly. This is called implicit casting.
The process of converting lower data type into higher data type is called widening in
java.
The conversion of a higher data type into a lower data type is called narrowing
conversion.
The process of converting a class type into another class type having relationship
between them through inheritance is called class casting.
When the method of superclass is overridden in the subclass to provide more specific
implementation, it is called method overriding.
We cannot override private method because when superclass method is private that will
not be visible to subclass, whatever methods we writing in the subclass will be treated as a
new method but not an overridden method.
37. Can we override static method in Java?
No, we cannot override a static method. We cannot also override a static method with
an instance method because static method is bound with class whereas an instance method is
bound with an object.
40. What are the differences between method overloading and method overriding?
A list of differences between overloading and overriding in Java is given below for quick
revision in tabular form.
SN Property Overloading Overriding
Private/Static/Final
5 Can be overloaded. Cannot be overridden.
method
Also known as
compile-time
Also known as runtime polymorphism,
9 Polymorphism polymorphism, static
dynamic polymorphism, or late binding.
polymorphism, or early
binding.
Abstraction is a technique by which we can hide the data that is not required to a user. It
hides all unwanted data so that users can work only with the required data.
43. What is the difference between abstraction and encapsulation?
Abstraction is used to hide the implementation details whereas, encapsulation binds
code and data into a single unit.
There are two ways by which we can achieve abstraction in java. They are:
o Abstract class (0 to 100%)
o Interface (100%)
A class that is declared with an abstract keyword is called abstract class. An abstract
class has to be extended and its abstract methods must be implemented by a child class.
46. What is abstract method in Java?
A method which is declared with abstract modifier and has no implementation (means
no body) is called an abstract method. It does not contain any body.
47. Can an abstract class be defined without any abstract methods?
No, if there is any abstract method in a class then class must be declared with abstract
keyword.
49. Is it allowed to declare a method abstract as well as final?
No, because abstract method needs to be overridden by child class whereas, a final
method cannot be overridden. Therefore, a method can be either abstract or final in Java.
50. Can we create an object of abstract class in Java?
No, we cannot create an instance of abstract class in Java. This is because abstract class
is not a concrete class. If you try to instantiate, you will get compile time error.
51. Can we create a reference for an abstract class?
Yes, we can create a reference for an abstract class only when the object being provided
the implementation for the abstract class.
52. When to use abstract class and abstract method in Java?
An abstract method is generally used when the same method has to perform different
tasks depending on the object calling it.
An abstract class is used when we need to share the same method to all non-abstract
subclasses with their own specific implementations.
53. What is interface in Java?
Yes, from Java 8 onwards, we can declare static and default methods in an interface.
Prior to Java 8, it was not allowed.
55. Can an interface be final?
No, because its implementation is provided by another class. A final method cannot be
overridden. Therefore, an interface method cannot be declared as final.
56. What is marker interface?
An interface that has no variable and method is known as marker interface. For
example, serializable, cloneable, remote, etc.
57. What is the difference between abstract class and interface?
We cannot create an object of abstract class but we can create an object of subclass of
abstract class. When we create an object of subclass of an abstract class, it calls the
constructor of subclass.
This subclass constructor has super in the first line that calls constructor of an abstract class.
Thus, the constructors of an abstract class are used from constructor of its subclass.
If the abstract class doesn’t have constructor, a class that extends that abstract class will not
get compiled.
61. Does Java allow us to declare private and protected modifiers for variables in an
interface?
The connecting (linking) between a method call and method body/definition is called
binding in java.
65. What is the difference between static binding and dynamic binding?
Final is a keyword that is used to restrict the user in Java programming. It is a non-
access modifier that can be applied to a variable, method, or class.
67. What is the final keyword in Java?
No, Java does not allow to change the value of final variable. Once the value is defined,
it cannot be changed.
69. Can a class be declared as final in Java?
Yes, a class can be declared as final in Java. Once a class is declared final, it cannot be
extended.
70. What is blank final variable? Can we initialize the blank final variable?
A variable that is declared as final and not initialized at a time of declaration is known
as a blank final variable.
Yes, if it is non-static then we can declare in the constructor. If it is static blank final variable,
it can be initialized only in the static block.
71. Can we change state of object to which a final reference is pointing?
Yes, we can change the state of an object to which a final reference variable is pointing
but we cannot re-assign a new object to this final reference variable.
72. Can we declare a method as final in Java?
Yes, we can declare a method as final but a final method cannot be overridden by a
child class.
73. How can inheritance be prohibited in Java?
If a class is declared as final, it cannot be extended. This will prevent the inheritance of
that class in Java.
74. Is it allowed to mark main method as final?
String class
Static is a keyword that is used for memory management mainly. Static means single
copy storage for variables or methods.
78. Can a class be declared as static?
No, a class cannot be marked as static but an inner class can be static.
79. What is the use of static keyword in Java?
The purpose of using static keyword in Java programming is that we can access the
data, method, or block of the class without any object creation.
80. Is it allowed to change the value of static variable in Java?
Yes, we can change the value of the static variable by using a constructor, or static
block but not inside a static method.
81. What is the difference between static variable and instance variable?
The difference between static variable (class variable) and instance variable are as
follows:
A static variable is also known as class variable whereas, instance variable is also
known as non-static variable.
Class variables can be accessed inside the static block, instance block, static method,
instance method, and method of inner class whereas instance variable can be accessed
inside the instance block, instance method, and method of inner class.
Class variable is always resolved during compile time whereas, instance variable is
resolved during the runtime.
They are not serialized in Java whereas, instance variables are serialized in Java.
In entire core java, this and super keyword is not allowed inside the static method or
static area.
83. Can static method be overloaded in Java?
Yes, a static method can be overloaded in Java but not override it.
84. In Java, why do we use static variable?
If we have a common property for all objects of a class, we use a class-level variable
i.e. a static variable. This variable is loaded in memory only once at the time of class loading.
So, it saves memory.
85. What is the difference between static method and instance method?
The difference between static method (class method) and instance method are as
follows:
A static method is also known as class method whereas instance method is also
known as non-static method.
The only static variable can be accessed inside static method whereas, in the instance
method, both static and instance variables can be accessed.
We do not need to create the object of the class for accessing static method whereas,
in the case of an instance method, we need to create the object for access.
Class method cannot be overridden whereas, an instance method can be overridden.
No, we cannot override static method because a static method is resolved at compile
time by Java compiler whereas method overriding is resolved at runtime by JVM because
objects are only available at runtime.
You can declare static methods with the same signature in subclass, but it is not considered as
overriding. It is considered a method hiding.
87. What is the use of static block in Java?
The purpose of using a static block is to write that logic inside static block that is
executed during the class loading. It is also used for changing the default value of static
variable.
The constructor is mainly used in a program to assign the default value of instance
variables.
89. What is the use private constructor in Java?
Private constructor is used to preventing other classes from accessing objects of a class.
It can be also used in single tone classes where the object of the class cannot be created
outside the class.
90. What is the difference between constructor and method?
Constructor overloading is a technique in Java in which a class can have more than one
constructor that differ in the parameters list.
93. What is constructor chaining in Java?
The purpose of using methods in the Java program is to write the logic of the
application which performs some specific task.
97. What is main method in Java?
A main() method is an entry point to start the execution of a program. Every Java
application has at least one class and at least one main method.
98. Why do we need to declare main method as static in Java?
The main method is declared as static. It is called by JVM when we run a class. JVM
does not know how to create an object of a class. It needs a standard way to start the
execution of a program.
Therefore, the main method is declared as static so that JVM can call it using the class name
which is passed on the command line.
99. Can we have more than one main() method in class?
Yes, a class can have any number of main() methods but the execution always starts
from public static void main(String[ ] args) only.
100. What is the difference between argument and parameter in Java?
If we make any constructor as a private, we cannot create the object of that class from
another class and also cannot create the subclass of that class.
106. Can we create an object of class if we make a constructor protected?
Yes, if we make constructor as protected then we can create the subclass of that class
within the same package but not outside the package.
107. Which modifiers are applicable to the outer class?
Public, default, final, abstract, and strictfp are five modifiers that can be applied to outer
class.
108. Which modifiers cannot be applied to the method?
Transient and volatile are two modifiers that cannot be applied to the method.
109. Which modifiers are not applicable to variables?
Only access modifiers are applicable to the constructor. Non-access modifiers cannot be
applied.
112. What are modifiers applicable to the outer interface?
Public, default, abstract, and strictfp are such modifiers that are applicable for outer
interface.
113. What are non-access modifiers in Java?
Abstract, final, native, static, strictfp, synchronized, transient, and volatile are non-
access modifiers in Java.
Core Java Interview Questions based on Class, Object, Data types, and Variables
Primitive data types are those data types whose variables can store only one value at a
time. Java defines eight primitive data types: boolean, char, byte, short, int, long, char, float,
and double.
115. What are the non-primitive data types in Java?
Non-primitive data types are created by programmers. These data types are used to
store a group of values or several values. Class, object, string, array, and interface are five
non-primitive data types in Java.
116. What is the default value of the local variable?
Java does not initialize local variable with any default value. Therefore, local variable
will be null by default.
117. What is the difference between an object and object reference?