Java OOP Interview Questions
Java is a pure object-oriented programming language (with minor exceptions like primitives). OOP is one of the most frequently tested topics in interviews. It models real-world entities using classes and objects and promotes code reusability, modularity and scalability.
1. What is an object-oriented paradigm and What are the main concepts of OOP in Java?
A paradigm means a method or style of programming. In programming, there are four main paradigms: Imperative, Logical, Functional and Object-Oriented. The object-oriented paradigm is based on using objects as the main entities. These objects can use features like encapsulation, inheritance, and polymorphism to build structured programs.
The main concepts of OOPs in Java are mentioned below:
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
2. What is the difference between an object-oriented programming language and an object-based programming language?
Object-Oriented Programming Language | Object-Based Programming Language |
|---|---|
| Object-oriented programming language covers larger concepts like inheritance, polymorphism, abstraction, etc. | The scope of object-based programming is limited to the usage of objects and encapsulation. |
| It supports all the built-in objects | It doesn’t support all the built-in objects |
| Examples: Java, C#, etc. | Examples: Java script, visual basics, etc. |
3. What are Classes in Java?
In Java, Classes are the collection of objects sharing similar characteristics and attributes. Classes represent the blueprint or template from which objects are created. Classes are not real-world entities but help us to create objects which are real-world entities. A class is declared using the class keyword. and It contains:
- Fields / Variables (data of an object)
- Methods (operations/functions)
- Constructors
- Nested classes
- Blocks (static & instance)
Example: Below program creates a Car object, sets its brand and speed, and then displays those details to the user.
class Car {
// fields
String brand;
int speed;
// method
void showDetails()
{
System.out.println(brand + " runs at " + speed
+ " km/h");
}
}
class GFG{
public static void main(String[] args)
{
// creating object
Car c1 = new Car();
c1.brand = "BMW";
c1.speed = 200;
c1.showDetails();
}
}
Output
BMW runs at 200 km/h
4. What is an object?
The object is a real-life entity that has certain properties and methods associated with it. The object is also defined as the instance of a class. An object can be declared using a new keyword.
Example:
class Dog{
String name;
void bark() {
System.out.println(name + " is barking!");
}
}
public class GFG {
public static void main(String[] args) {
Dog d1 = new Dog(); // object created
d1.name = "Tommy"; // setting value
d1.bark(); // calling method
}
}
Output
Tommy is barking!
Explanation: d1 is an object of the Dog class that stores data and performs actions.
5. What are the different ways to create objects in Java?
Methods to create objects in Java are mentioned below:
- Using new keyword
- Using new instance
- Using clone() method
- Using deserialization
- Using the newInstance() method of the Constructor class
To know more about methods to create objects in Java refer to this article.
6. How is the ‘new’ operator different from the ‘newInstance()’ operator in Java?
- The new operator is used to create objects at compile-time.
- Type is known beforehand.
Example:
Car c = new Car(); // using new
- newInstance() operator creates an object at runtime using reflection.
- Type is decided dynamically.
- Throws exceptions (e.g., InstantiationException).
Example:
Car c = Car.class.newInstance(); // using newInstance()
7. What is the difference between static (class) method and instance method?
Static(Class) method | Instance method |
|---|---|
Static method is associated with a class rather than an object. | The instance method is associated with an object rather than a class. |
Static methods can be called using the class name only without creating an instance of a class. | The instance methods can be called on a specific instance of a class using the object reference. |
Static methods do not have access to this keyword. | Instance methods have access to this keyword. |
Static methods can access only static members of the class. | Instance methods can access both static and non-static methods of the class. |
Static methods can’t be overridden (resolved at compile-time by reference type). | While instance methods can be overridden (resolved at run-time by object type). |
8. What is this keyword in Java?

‘this’ is a keyword used to reference a variable that refers to the current object.
9. What are the advantages and disadvantages of object cloning?
There are many advantages and disadvantages of using object cloning as mentioned below:
Advantages:
- Unlike = which copies only references, clone() creates a new object copy.
- Reduces code size since manual field copying is avoided.
- Useful for replicating complex objects (similar to Prototype pattern).
Disadvantages:
- clone() is protected, so classes must override it and implement Cloneable.
- By default, it performs a shallow copy deep copy needs custom implementation.
- Can be tricky to maintain if objects have nested references.
10. What is the difference between shallow cloning and deep cloning in Java?
- Shallow cloning: Shallow cloning copies only the object’s top-level structure, meaning nested objects are shared between the original and the clone.
- Deep cloning: Deep cloning creates a completely independent copy, including all nested objects.
11. What are Access Specifiers and Types of Access Specifiers?

Access Specifiers in Java help to restrict the scope of a class, constructor, variable, method, or data member. There are four types of Access Specifiers in Java mentioned below:
- Public
- Private
- Protected
- Default
12. What will be the initial value of an object reference which is defined as an instance variable?
The initial value of an object reference which is defined as an instance variable is a NULL value.
13. What is the constructor?
Constructor is a special method that is used to initialize objects. Constructor is called when a object is created. The name of constructor is same as of the class.
- Purpose: To initialize the object’s state (i.e., assign values to instance variables) when an object is created.
- Name: Must be the same as the class name.
- No return type: Constructors do not have a return type.
- Automatically called: It is called automatically when the new keyword is used.
- Can be overloaded: We can define multiple constructors with different parameter
Example:
class XYZ{
private int val;
// Constructor
XYZ(){
val=0;
}
};
14. How many types of constructors are used in Java.
There are four types of constructors in Java

- Default Constructor: It is the type that does not accept any parameter value. It is used to set initial values for object attributes.
- Parameterized Constructor: It is the type of constructor that accepts parameters as arguments. These are used to assign values to instance variables during the initialization of objects.
- Copy Constructor: Unlike other constructors copy constructor is passed with another object which copies the data available from the passed object to the newly created object.
- Private Constructor: A private constructor in Java is a constructor declared with the private keyword, which cannot be accessed from outside the class, preventing direct object creation.
15. What happens if we don't provide constructor in class?
If you don't provide a constructor in a class in Java, the compiler automatically generates a default constructor with no arguments and no operation which is a default constructor.
16. What is the purpose of a default constructor?
Constructors help to create instances of a class or can be said to create objects of a class. Constructor is called during the initialization of objects. A default constructor is a type of constructor which do not accept any parameter, So whatever value is assigned to properties of the objects are considered default values.
17. Where and how can you use a private constructor?
A private constructor is used if you don't want any other class to instantiate the object to avoid subclassing. The use private constructor can be seen as implemented in the example.
Example: Java program to demonstrate implementation of Singleton pattern using private constructors.
import java.io.*;
class GFG {
static GFG instance = null;
public int x = 10;
// private constructor can't be accessed outside the class
private GFG() {}
// Factory method to provide the users with instances
static public GFG getInstance()
{
if (instance == null)
instance = new GFG();
return instance;
}
}
// Driver Class
class Main {
public static void main(String args[])
{
GFG a = GFG.getInstance();
GFG b = GFG.getInstance();
a.x = a.x + 10;
System.out.println("Value of a.x = " + a.x);
System.out.println("Value of b.x = " + b.x);
}
}
Output
Value of a.x = 20 Value of b.x = 20
18. What are the differences between the constructors and methods?
| Difference | Constructor | Method |
|---|---|---|
| Purpose | Creates/initializes an object | Performs an action or operation |
| Name | Same as class name | Any valid name |
| Return type | No return type | Has a return type (can be void) |
| Called when | Automatically when object is created | Manually when needed |
| Default provided by compiler | Yes, if none is defined | No, must be defined explicitly |
Code Example:
public class Person {
String name;
// Constructor
public Person(String personName) {
name = personName;
System.out.println("Constructor called: Object created");
}
// Method
public void greet() {
System.out.println("Hello, my name is " + name);
}
// Main method to run the program
public static void main(String[] args) {
// Creating an object, which calls the constructor
Person p = new Person("Alice");
// Calling a method on the object
p.greet();
}
}
Explanation:
- Person(String personName) Constructor: called automatically when new Person("Alice") is used
- greet() Method: called manually using p.greet()
19. What do you mean by data encapsulation?

Encapsulation is one of the core principles of Object-Oriented Programming (OOP). It is the practice of wrapping the data (variables) and methods that operate on the data into a single unit (class), while restricting direct access to some of the object’s components.
20. What are the advantages of Encapsulation in Java?
The advantages of Encapsulation in Java are mentioned below:
- Data Hiding: it is a way of restricting the access of our data members by hiding the implementation details. Encapsulation also provides a way for data hiding. The user will have no idea about the inner implementation of the class.
- Increased Flexibility: We can make the variables of the class read-only or write-only depending on our requirements.
- Reusability: Encapsulation also improves the re-usability and is easy to change with new requirements.
- Testing code is easy: Code is made easy to test for unit testing.
21. How do you implement Encapsulation?
Steps to implement Encapsulation:
- Declare class variables as private.
- Provide public getter and setter methods to access and update the values.
Example: Java Program to demonstrate use of Encapsulation
import java.io.*;
class Person {
private String Name;
private int age;
public String getName() { return Name; }
public void setName(String Name) { this.Name = Name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}
// Driver class
class GFG {
// main function
public static void main(String[] args)
{
Person p = new Person();
p.setName("Rohan");
p.setAge(29);
System.out.println("Name is " + p.getName());
System.out.println("Age is " + p.getAge());
}
}
Output
Name is Rohan Age is 29
22. What is the use of Getters and Setters?
Getters are methods used to read private variables and Setters are methods used to update private variables safely. They provide controlled access to private data.
emp.setAge(30); // Setter
int age = emp.getAge(); // Getter
23. What is inheritance in Java?
Inheritance is an OOP concept that allows a class (called subclass/child class) to acquire properties and behaviors (fields and methods) of another class (called superclass/parent class).
Example: In the following example, Animal is the base class and Dog, Cat and Cow are derived classes that extend the Animal class.

Example:
// Parent class
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
// Child class
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
// Child class
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
// Child class
class Cow extends Animal {
void sound() {
System.out.println("Cow moos");
}
}
// Main class
public class Geeks {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound();
a = new Cat();
a.sound();
a = new Cow();
a.sound();
}
}
Output
Dog barks Cat meows Cow moos
24. What are the different types of inheritance in Java
Java supports the following types of inheritance:
- Single Inheritance: When a child or subclass extends only one superclass, it is known to be single inheritance. Single-parent class properties are passed down to the child class.
- Multilevel Inheritance: When a child or subclass extends any other subclass a hierarchy of inheritance is created which is known as multilevel inheritance. In other words, one subclass becomes the parent class of another.
- Hierarchical Inheritance: When multiple subclasses derive from the same parent class is known as Hierarchical Inheritance. In other words, a class that has a single parent has many subclasses.
- Multiple Inheritance: When a child class inherits from multiple parent classes is known as Multiple Inheritance. In Java, it only supports multiple inheritance of interfaces, not classes.
25. What is multiple inheritance? Is it supported by Java?
A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from many parent classes. When methods with the same signature are present in both superclasses and subclasses, an issue arises. The method's caller cannot specify to the compiler which class method should be called or even which class method should be given precedence.
Note: Java doesn’t support Multiple Inheritance
Example: Java Program to show multiple Inheritance
import java.io.*;
interface Animal {
void eat();
}
interface Mammal {
void drink();
}
class Dog implements Animal, Mammal {
public void eat() { System.out.println("Eating"); }
public void drink() { System.out.println("Drinking"); }
void bark() { System.out.println("Barking"); }
}
class GFG {
public static void main(String[] args)
{
Dog d = new Dog();
d.eat();
d.drink();
d.bark();
}
}
Output
Eating Drinking Barking
26. Is there any limitation to using Inheritance?
Yes, there is a limitation of using Inheritance in Java, as because of inheritance one can inherit everything from super class and interface because of which subclass is too clustered and sometimes error-prone when dynamic overriding or dynamic overloading is done in certain situations.
27. What is the ‘IS-A ‘ relationship in OOPs Java?
'IS-A' is a type of relationship in OOPs Java where one class inherits another class.
28. What is HAS-A relationship in OOP (Aggregation/Composition)?
The HAS-A relationship represents composition or aggregation. It shows that one class contains another class as a field.
class Engine {
void start() { System.out.println("Engine started"); }
}
class Car {
private Engine engine = new Engine(); // Car HAS-A Engine
void startCar() { engine.start(); }
}
- Aggregation: “Has-a” relationship where the contained object can exist independently.
- Composition: “Has-a” relationship where the contained object cannot exist without the container.
29. What do you mean by aggregation?

Aggregation is a term related to the relationship between two classes best described as a "has-a" relationship. This kind is the most specialized version of association. It is a unidirectional association means it is a one-way relationship. It contains the reference to another class and is said to have ownership of that class.
30. What is an association?
The association is a relation between two separate classes established through their Objects. It represents Has-A’s relationship.
31. What is the composition of Java and State the difference between Composition and Aggregation?
Composition in Java is a design principle where one class contains an instance of another class and establishes a strong relationship between them. The child object cannot exist independently of the parent object.
Example:
A Human has a Heart, but a Heart cannot exist without a Human.
Aggregation | Composition |
|---|---|
It defines a "has a" relationship between the objects | It represents the part-of relationship |
Objects are independent of each other. | Objects are dependent on each other. |
Represent it by using the empty diamond. | Represent it by using the filled diamond. |
Child objects don't have a lifetime. | Child objects have a lifetime. |
32. What is Polymorphism?
Polymorphism is an OOP concept that allows one interface or method to take many forms. In simple words, a single entity (method or object) can behave differently in different situations.
- The term comes from Greek: “poly” = many, “morph” = forms.
- Polymorphism improves code flexibility, reusability, and maintainability.
Example: Below example describe the same method sound() behaves differently depending on the object type -> polymorphism.
class Animal {
void sound() {
System.out.println("Some animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound(); // Output: Bark
a = new Cat();
a.sound(); // Output: Meow
}
}
Output
Bark Meow
33. How many types of Polymorphism.
There are two types of polymorphism in java:
- Compile-time (Static) Polymorphism
- Runtime (Dynamic) Polymorphism

32. What is Compile-time (Static) Polymorphism?
Compile-Time Polymorphism in Java is also known as static polymorphism and also known as method overloading. This happens when multiple methods in the same class have the same name but different parameters. and it is used to achieved using method overloading
Example: Method Overloading
class Calculator{
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
public class GFG{
public static void main(String[] args)
{
Calculator calc = new Calculator();
System.out.println(calc.add(5, 10)); // Output: 15
System.out.println(
calc.add(5.5, 3.2)); // Output: 8.7
}
}
Output
15 8.7
33. What is Runtime (Dynamic) Polymorphism?
Runtime Polymorphism in Java known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding. Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
Example: Method Overriding
class Animal{
void sound()
{
System.out.println("Some animal sound");
}
}
class Dog extends Animal{
@Override void sound() { System.out.println("Bark"); }
}
public class GFG{
public static void main(String[] args)
{
Animal a = new Dog();
a.sound(); // Output: Bark (decided at runtime)
}
}
Output
Bark
34. What is method overriding and method overloading?
Method Overloading: In Java, Method Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters, or a mixture of both.
Method overriding,: When a subclass provides its own implementation of a method that is already defined in its parent class, it is called method overriding.

Method overloading in Java is also known as Compile-time Polymorphism, Static Polymorphism, or Early binding. In Method overloading compared to the parent argument, the child argument will get the highest priority.
35. Can we override the static method ?
No, as static methods are part of the class rather than the object so we can't override them.
36. Can we change the scope of the overridden method in the subclass?
Yes, we can change the scope of an overridden method in the subclass, but only to make it wider or the same as the superclass method’s scope.
- If the overridden method in the superclass is public, the subclass method must be public (it cannot be protected, default, or private).
- If the overridden method in the superclass is protected, the subclass method can be protected or public, but not private or default.
- If the overridden method in the superclass has default (package-private) access, the subclass method can be default, protected, or public, but not private.
- A private method cannot be overridden because it is not visible to the subclass.
37. What is Abstraction?
Abstraction in Java is the process of hiding internal implementation details and showing only essential functionality to the user. It focuses on what an object does rather than how it does it.
- Abstraction hides the complex details and shows only essential features.
- Abstract classes may have methods without implementation and must be implemented by subclasses.
- By abstracting functionality, changes in the implementation do not affect the code that depends on the abstraction.
38. How many ways to achieve abstraction in Java.
Java provides two ways to implement abstraction, which are listed below:
- Abstract Classes (Partial Abstraction)
- Interface (100% Abstraction)
39. What is Abstract class?
A class declared as abstract, cannot be instantiated i.e., the object cannot be created. It may or may not contain abstract methods but if a class has at least one abstract method, it must be declared abstract.
Example of an abstract class with abstract method:
import java.io.*;
// Abstract class
abstract class Fruits {
abstract void run();
}
// Driver Class
class Apple extends Fruits {
void run()
{
System.out.println("Abstract class example");
}
// main method
public static void main(String args[])
{
Fruits obj = new Apple();
obj.run();
}
}
Output
Abstract class example
An abstract method is used when we want to use a method but want to child classes to decide the implementation in that case we use Abstract methods with the parent classes.
40. What is an Interface?
An interface in Java is a collection of static final variables and abstract methods that define the contract or agreement for a set of linked classes. Any class that implements an interface is required to implement a specific set of methods. It specifies the behavior that a class must exhibit but not the specifics of how it should be implemented.
Syntax:
interface
{
// constant fields
// methds that are abstract by default
}
Example: Java Program to demonstrate Interface
import java.io.*;
interface Shape {
double getArea();
double getPerimeter();
}
class Circle implements Shape {
private double radius;
public Circle(double radius) { this.radius = radius; }
public double getArea()
{
return Math.PI * radius * radius;
}
public double getPerimeter()
{
return 2 * Math.PI * radius;
}
}
class GFG {
public static void main(String[] args)
{
Circle circle = new Circle(5.0);
System.out.println("Area of circle is "
+ circle.getArea());
System.out.println("Perimeter of circle is"
+ circle.getPerimeter());
}
}
Output
Area of circle is 78.53981633974483 Perimeter of circle is31.41592653589793
41. Give some features of the Interface.
An Interface in Java programming language is defined as an abstract type used to specify the behavior of a class. An interface in Java is a blueprint of a behavior. A Java interface contains static constants and abstract methods.
Features of the Interface are mentioned below:
- The interface can help to achieve total abstraction.
- Allows us to use multiple inheritances in Java.
- Any class can implement multiple interfaces even when one class can extend only one class.
- It is also used to achieve loose coupling.
42. What is a marker interface?
An Interface is recognized as an empty interface (no field or methods) it is called a marker interface. Examples of marker interfaces are Serializable, Cloneable and Remote interfaces.
43. What are the differences between abstract class and interface?

Abstract Class | Interface Class |
|---|---|
Both abstract and non-abstract methods may be found in an abstract class. | Can have abstract, default, static (Java 8+) and private methods (Java 9+). |
Abstract Class supports Final methods. | The interface class does not support Final methods. |
Multiple inheritance is not supported by the Abstract class. | Multiple inheritances is supported by Interface Class. |
Abstract Keyword is used to declare Abstract class. | Interface Keyword is used to declare the interface class. |
| extend keyword is used to extend an Abstract Class. | implements keyword is used to implement the interface. |
Abstract Class has members like protected, private, etc. | All class members are public by default. |