Java OOPs Interview Questions And Answers

    Java OOPs Interview Questions And Answers

    Crack Java interviews with these 20 OOP interview questions and answers. Learn key concepts like inheritance, polymorphism, abstraction, and more with clear examples.

    default profile

    Munaf Badarpura

    July 14, 2025

    10 min read

    If you're preparing for Java interviews, one of the most important topics you’ll definitely come across is OOP - Object Oriented Programming. Almost everything in Java revolves around objects and classes. So understanding the core OOP concepts is a must for interview.

    In this article, I’ve shared 20 commonly asked Java OOP interview questions in simple words. I have included code snippets where they make sense to clear things up. Let’s dive into the questions and answers.

    1. What is an object-oriented paradigm?#

    An object-oriented paradigm is a way of designing and writing code based on real-world objects.

    Instead of just writing a bunch of functions, you create objects that have their own data (like properties) and behavior (like methods). Think of it as organizing your code like real-world objects—a car has wheels and can drive, a dog has a name and can bark.

    It makes your code more modular, reusable, and easier to maintain. Java is built around this idea, using concepts like inheritance, polymorphism, and encapsulation to structure programs.

    2. Why do we need OOPs?#

    OOP makes life easier for developers! Here’s why:

    • Modularity: You can break your program into smaller, self-contained objects, making it easier to manage.
    • Reusability: Write a class once, and you can use it everywhere (like a blueprint).
    • Scalability: Big projects are easier to handle because OOP organizes code logically.
    • Maintainability: Fixing or updating code is simpler since changes are often limited to specific classes.
    • Real-world modeling: It mimics how we think about the world (objects like cars, people, etc.), so it’s intuitive.

    Without OOP, you’d end up with messy, hard-to-read code—especially for large projects.

    3. What is a Class in Java?#

    A class is like a blueprint for creating objects. It defines what an object can do (methods) and what data it holds (fields). For example, if you’re modeling a car, the class would define its properties (like color or speed) and behaviors (like drive or stop).

    Here’s a simple example:

    public class Car { String color; // Property int speed; void drive() { // Behavior System.out.println("Car is driving at " + speed + " km/h"); } }

    You can create multiple cars from this class, each with its own color and speed.

    4. What is an Object in Java?#

    An object is an instance of a class. Think of the class as the blueprint and the object as the actual thing built from it. For example, if Car is the class, an object would be a specific car, like a red Toyota going 60 km/h.

    Here’s how you create an object:

    public class Main { public static void main(String[] args) { Car myCar = new Car(); // Creating an object myCar.color = "Red"; myCar.speed = 60; myCar.drive(); // Output: Car is driving at 60 km/h } }

    5. What are the Features of OOPs?#

    OOP in Java revolves around four main pillars:

    1. Encapsulation: Bundling data and methods together in a class and controlling access (like using private fields and public getters/setters).
    2. Inheritance: One class can inherit properties and methods from another (like a child inheriting traits from a parent).
    3. Polymorphism: Objects can take on multiple forms—like a method behaving differently based on the object calling it.
    4. Abstraction: Hiding complex details and showing only the necessary parts (like using an interface to simplify interactions).

    These make your code flexible, secure, and easier to work with.

    6. What is a Constructor and Types of Constructors?#

    A constructor is a special method in a class that gets called when you create an object. It’s used to initialize the object’s properties. Constructors have the same name as the class and no return type.

    There are two types of constructors:

    1. Default Constructor: Automatically provided by Java if you don’t define any. It initializes fields to default values (like 0 or null).
    public class Student { String name; Student() { // Default constructor name = "Unknown"; } }
    1. Parameterized Constructor: Lets you pass values to initialize the object.
    public class Student { String name; int age; Student(String n, int a) { // Parameterized constructor name = n; age = a; } }

    7. What is a Static Variable?#

    A static variable belongs to the class, not to any specific object. It’s shared across all instances of the class. For example, if you want to count how many objects of a class are created, you’d use a static variable.

    Here’s an example:

    public class Counter { static int count = 0; // Static variable Counter() { count++; } public static void main(String[] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); System.out.println("Total objects: " + Counter.count); // Output: Total objects: 2 } }

    8. What is the this Keyword in Java?#

    The this keyword refers to the current object. It’s super useful when you need to differentiate between instance variables and parameters with the same name.

    For example:

    public class Person { String name; Person(String name) { this.name = name; // 'this.name' refers to the instance variable } }

    Here, this.name refers to the class’s name field, while name is the parameter passed to the constructor.

    9. What is Constructor Chaining in Java?#

    Constructor chaining is when one constructor calls another constructor in the same class or a parent class. It’s a way to reuse code and avoid duplicating initialization logic.

    You can chain constructors using this() (for the same class) or super() (for the parent class).

    Example:

    public class Employee { String name; int id; Employee() { this("Unknown", 0); // Calls parameterized constructor } Employee(String name, int id) { this.name = name; this.id = id; } }

    Here, the default constructor calls the parameterized constructor to set default values.

    10. What is Inheritance?#

    Inheritance lets one class inherit the properties and methods of another class. The class that inherits is called the child or subclass, and the class it inherits from is the parent or superclass. It’s great for code reuse.

    Example:

    class Animal { void eat() { System.out.println("This animal eats food"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks"); } }

    A Dog object can use both eat() (from Animal) and bark().

    11. Why is Multiple Inheritance Not Supported in Java?#

    Java doesn’t allow a class to inherit from multiple classes (multiple inheritance) to avoid complexity and ambiguity. For example, if two parent classes have methods with the same name, which one should the child class use? This is called the diamond problem.

    Instead, Java supports multiple inheritance through interfaces. A class can implement multiple interfaces, which don’t cause the same conflicts since they only define method signatures.

    12. What is super in Java?#

    The super keyword refers to the parent class. You can use it to:

    • Call the parent class’s constructor (super()).
    • Access the parent class’s methods or fields.

    Example:

    class Animal { String name = "Animal"; } class Dog extends Animal { String name = "Dog"; void printNames() { System.out.println(name); // Dog System.out.println(super.name); // Animal } }

    Here, super.name accesses the parent class’s name field.

    13. What is Data Abstraction in Java?#

    Data abstraction is about hiding the complex implementation details and showing only the necessary parts to the user. In Java, you achieve this using abstract classes or interfaces.

    For example, you don’t need to know how a List works internally to use it, you just call methods like add() or remove().

    Example with an abstract class:

    abstract class Shape { abstract void draw(); // Abstract method } class Circle extends Shape { void draw() { System.out.println("Drawing a circle"); } }

    The user only interacts with draw(), not the internal logic.

    14. What are the Differences Between Abstraction and Encapsulation?#

    • Abstraction: Focuses on hiding complexity and showing only what’s needed. It’s about the “what” (like interfaces or abstract classes).
    • Encapsulation: Focuses on bundling data and methods together and controlling access (like using private fields with public getters/setters). It’s about the “how.”

    Think of abstraction as designing the interface of a car (steering, pedals), while encapsulation is hiding the engine’s inner workings.

    15. What is Polymorphism and Types of Polymorphism?#

    Polymorphism means “many forms.” It lets objects of different classes be treated as objects of a common parent class. There are two types:

    1. Compile-time Polymorphism (Method Overloading): Same method name, different parameters in the same class.
    class Calculator { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }
    1. Runtime Polymorphism (Method Overriding): A subclass provides its own implementation of a parent class’s method.
    class Animal { void sound() { System.out.println("Some sound"); } } class Cat extends Animal { void sound() { System.out.println("Meow"); } }

    16. Difference Between Method Overloading and Method Overriding in Java?#

    • Method Overloading:
      • Happens in the same class.
      • Same method name, different parameters (number or type).
      • Resolved at compile time.
      • Example: add(int, int) and add(int, int, int).
    • Method Overriding:
      • Happens in a subclass.
      • Same method name and signature as the parent class.
      • Resolved at runtime.
      • Example: Animal’s sound() overridden by Cat’s sound().

    17. What is the final Keyword in Java?#

    The final keyword in Java is used to restrict modification. It can be applied to:

    • Variables: Makes them constant (can’t be changed once set).
    • Methods: Prevents them from being overridden in subclasses.
    • Classes: Prevents them from being extended.

    Example:

    final class Immutable { final int value = 10; // Constant variable final void display() { // Can't be overridden System.out.println("Value: " + value); } }

    If you try to extend Immutable or override display(), Java will throw an error.

    18. Can we make the Interface final?#

    No, we can’t make an interface final in Java.

    Because final means you can't extend or inherit it. But the whole point of an interface is to be implemented by other classes. If we make it final, no class would be able to implement it — which completely breaks the purpose of having an interface.

    So yeah, the compiler won’t even allow it.

    19. What is a marker interface?#

    A marker interface is a special type of interface that doesn’t have any methods.

    Its main job is to mark or tag a class so that the JVM or frameworks know something special about that class.

    For example, Serializable is a marker interface. When you make your class implement it, Java knows that your object can be converted into a byte stream.

    java CopyEdit public class Student implements Serializable { // no methods to implement }

    So even though you don't write any methods, just by adding the interface, you're giving extra information about the class.

    20. What is Method Hiding in Java?#

    Method hiding happens when a subclass defines a static method with the same name and signature as a static method in the parent class. Unlike method overriding (which works with instance methods), the subclass method hides the parent class’s method, and the method called depends on the reference type, not the object type.

    Example:

    class Parent { static void show() { System.out.println("Parent's static method"); } } class Child extends Parent { static void show() { System.out.println("Child's static method"); } } class Main { public static void main(String[] args) { Parent.show(); // Output: Parent's static method Child.show(); // Output: Child's static method } }

    Conclusion#

    That was a lot, but I hope it feels clearer now! Java’s OOP concepts are the backbone of the language, and understanding them will make you shine in interviews. Practice these questions, play around with the code snippets, and you’ll be ready to tackle any OOP question that comes your way.

    Java OOP Interview Questions
    Object Oriented Programming in Java
    Java OOPs Concepts with Examples
    Java Beginner Interview Preparation

    More articles