OOPs Interview Questions for Freshers (with Java Examples)

Top OOPs interview questions for freshers with clear answers, Java code examples and outputs: the 4 pillars, overloading vs overriding, abstract class vs interface and tricky questions.

Why OOPs Questions Come Up in Every Technical Interview

Object-Oriented Programming (OOP) is the most common theory topic in fresher technical interviews. Whether you face TCS, Infosys, Wipro, Accenture, Cognizant or a product company, the interviewer is very likely to ask about classes, objects and the four pillars of OOP. Many interviews also ask you to write a small program or predict the output of one.

The questions themselves are not difficult, but freshers lose marks by giving textbook definitions without examples. A strong answer has three parts: a one-line definition, a real-world example and a small code snippet. This guide gives you all three for the most frequently asked questions.

All examples use Java, the language most interviewers use for OOP questions in India. Every program in this guide has been compiled and run, and its output is shown below it. The concepts are the same in C++ and Python, with small syntax differences.

The 4 Pillars of OOP at a Glance

PillarOne-line meaningReal-world exampleJava feature
EncapsulationBundle data and methods together and hide the dataAn ATM hides the bank's internal balance logicprivate fields + public methods
InheritanceA class reuses properties of another classA car is a vehicle and gets all vehicle featuresextends, implements
PolymorphismOne name, many formsA person is a student in class and a customer in a shopOverloading, overriding
AbstractionShow what an object does, hide how it does itYou drive a car without knowing how the engine worksabstract class, interface

Memorise this table. If you can explain each row with its example, you can answer most opening OOP questions confidently.

Basic OOPs Interview Questions

Q1. What is Object-Oriented Programming?

OOP is a programming approach that organises code around objects, which combine data (fields) and behaviour (methods). It makes large programs easier to maintain, reuse and extend. Java, C++, Python and C# all support OOP.

Q2. What is the difference between a class and an object?

A class is a blueprint, and an object is a real instance created from it. For example, Car is a class, while your friend's red Swift is an object. A class takes no memory for its data until an object is created with the new keyword.

Q3. How is OOP different from procedural programming?

Procedural programming (like C) organises code as a sequence of functions that act on separate data. OOP keeps data and the methods that use it together inside objects. This gives better data security through encapsulation and easier reuse through inheritance.

Q4. What is a constructor? What types are there?

A constructor is a special method that runs automatically when an object is created. It has the same name as the class and no return type. Java has a default constructor (added by the compiler if you write none), a no-argument constructor and a parameterised constructor.

Q5. What is the difference between the this and super keywords?

this refers to the current object, often used to separate a field from a parameter with the same name (this.r = r). super refers to the parent class. It is used to call the parent constructor, super(), or a parent method that was overridden.

Encapsulation

Encapsulation means keeping fields private and allowing access only through public methods. This protects data from invalid changes. In the example below, no outside code can set a negative balance, because the only way to change balance is through deposit(), which validates the amount.

Java
class BankAccount {
    private double balance;  // hidden from outside code

    public void deposit(double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("Amount must be positive");
        }
        balance += amount;
    }

    public double getBalance() {
        return balance;
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount acc = new BankAccount();
        acc.deposit(500);
        // acc.balance = 1000000;  // compile error: balance is private
        System.out.println(acc.getBalance());
    }
}

Output: 500.0

Q6. What are access modifiers in Java?

Access modifiers control where a class member can be used:

ModifierSame classSame packageSubclass (other package)Everywhere
privateYesNoNoNo
default (no keyword)YesYesNoNo
protectedYesYesYesNo
publicYesYesYesYes

Inheritance

Inheritance lets a child class reuse fields and methods from a parent class using the extends keyword. It represents an "is-a" relationship: a Dog is an Animal. Notice in the output that the parent constructor always runs before the child constructor.

Java
class Animal {
    Animal() { System.out.println("Animal constructor"); }
    void eat() { System.out.println("Animal eats"); }
}

class Dog extends Animal {
    Dog() { System.out.println("Dog constructor"); }
    void bark() { System.out.println("Dog barks"); }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();   // inherited from Animal
        d.bark();
    }
}

Output: Animal constructor
Dog constructor
Animal eats
Dog barks

Q7. What types of inheritance are there?

Single (one parent, one child), multilevel (A → B → C), hierarchical (one parent, many children), multiple (one child, many parents) and hybrid (a mix). Java supports single, multilevel and hierarchical inheritance with classes. Multiple inheritance is supported only through interfaces.

Q8. Why does Java not support multiple inheritance with classes?

To avoid the diamond problem. If classes B and C both inherit from A and override the same method, a class D extending both would not know which version to use. Java avoids this by allowing only one parent class. A class can still implement many interfaces. If two interfaces have the same default method, the class must override it and choose, as shown below.

Java
interface A {
    default void hello() { System.out.println("Hello from A"); }
}

interface B {
    default void hello() { System.out.println("Hello from B"); }
}

class C implements A, B {
    @Override
    public void hello() {
        A.super.hello();  // the class must resolve the conflict itself
    }
}

public class Main {
    public static void main(String[] args) {
        new C().hello();
    }
}

Output: Hello from A

Polymorphism

Polymorphism means one name with many forms. Java has two types: compile-time polymorphism (method overloading) and runtime polymorphism (method overriding).

Method Overloading (Compile-Time)

Several methods in the same class share a name but differ in the number or type of parameters. The compiler picks the right one.

Java
class Calculator {
    int add(int a, int b) { return a + b; }
    int add(int a, int b, int c) { return a + b + c; }
    double add(double a, double b) { return a + b; }
}

public class Main {
    public static void main(String[] args) {
        Calculator c = new Calculator();
        System.out.println(c.add(2, 3));      // 5
        System.out.println(c.add(2, 3, 4));   // 9
        System.out.println(c.add(2.5, 3.5));  // 6.0
    }
}

Output: 5
9
6.0

Method Overriding (Runtime)

A child class gives its own version of a method already defined in the parent. Java decides which version to call at runtime, based on the actual object, not the reference type.

Java
abstract class Shape {
    abstract double area();
}

class Circle extends Shape {
    double r;
    Circle(double r) { this.r = r; }
    @Override double area() { return Math.PI * r * r; }
}

class Rectangle extends Shape {
    double l, w;
    Rectangle(double l, double w) { this.l = l; this.w = w; }
    @Override double area() { return l * w; }
}

public class Main {
    public static void main(String[] args) {
        Shape[] shapes = { new Circle(1), new Rectangle(2, 3) };
        for (Shape s : shapes) {
            System.out.printf("%.2f%n", s.area());  // decided at runtime
        }
    }
}

Output: 3.14
6.00

Q9. What is the difference between overloading and overriding?

This is one of the most frequently asked OOP questions:

PointOverloadingOverriding
WhereSame classParent and child class
ParametersMust differMust be the same
Return typeCan differ, but cannot be the only differenceSame, or a subtype (covariant)
Decided atCompile timeRuntime
Static methodsCan be overloadedCannot be overridden (only hidden)

Q10. Can we overload a method by changing only its return type?

No. int add(int a, int b) and double add(int a, int b) cause a compile error, because a call like add(2, 3) would be ambiguous. The parameter list must differ.

Abstraction

Abstraction hides implementation details and shows only what an object can do. The Shape example above is abstraction with an abstract class. The example below uses an interface: the checkout method works with any payment type without knowing how each one works.

Java
interface Payment {
    void pay(int amount);
}

class UpiPayment implements Payment {
    public void pay(int amount) { System.out.println("Paid Rs " + amount + " via UPI"); }
}

class CardPayment implements Payment {
    public void pay(int amount) { System.out.println("Paid Rs " + amount + " via card"); }
}

public class Main {
    static void checkout(Payment method, int amount) {
        method.pay(amount);  // caller does not need to know how payment works
    }

    public static void main(String[] args) {
        checkout(new UpiPayment(), 499);
        checkout(new CardPayment(), 1299);
    }
}

Output: Paid Rs 499 via UPI
Paid Rs 1299 via card

Q11. What is the difference between an abstract class and an interface?

Another very common question. Since Java 8, interfaces can also have default and static methods, so mention that in your answer.

PointAbstract classInterface
MethodsAbstract and concreteAbstract, plus default and static (Java 8+)
VariablesAny typeOnly public static final constants
ConstructorYesNo
InheritanceA class can extend only oneA class can implement many
Use whenClasses share code and stateUnrelated classes share a capability

Tricky OOPs Questions Interviewers Love

Q12. Can we override a static method?

No. A static method belongs to the class, not the object. If a child defines the same static method, it hides the parent's version. The call is decided by the reference type, as this program shows:

Java
class Parent {
    static void greet() { System.out.println("Parent static"); }
    void show() { System.out.println("Parent instance"); }
}

class Child extends Parent {
    static void greet() { System.out.println("Child static"); }
    @Override void show() { System.out.println("Child instance"); }
}

public class Main {
    public static void main(String[] args) {
        Parent p = new Child();
        p.greet();  // static: decided by reference type, so Parent
        p.show();   // instance: decided by object type, so Child
    }
}

Output: Parent static
Child instance

Q13. Can a constructor be private?

Yes. A private constructor stops other classes from creating objects directly. It is used in the Singleton pattern, where only one object of the class should exist. Note that this simple version is not thread-safe; mention that if the interviewer asks a follow-up.

Java
class Config {
    private static Config instance;

    private Config() { }  // outside code cannot call new Config()

    static Config getInstance() {
        if (instance == null) {
            instance = new Config();
        }
        return instance;
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(Config.getInstance() == Config.getInstance());  // true
    }
}

Output: true

Q14. What does the final keyword do?

A final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be extended. The String class in Java is final.

Q15. What is the difference between composition and inheritance?

Inheritance is an "is-a" relationship (a Dog is an Animal). Composition is a "has-a" relationship (a Car has an Engine). Composition is often preferred because it keeps classes loosely connected and easier to change.

Q16. What is the contract between equals() and hashCode()?

If two objects are equal according to equals(), they must return the same hashCode(). Two objects with the same hash code do not have to be equal. If you override equals() but not hashCode(), objects may behave incorrectly in a HashMap or HashSet.

Q17. What is the difference between a shallow copy and a deep copy?

A shallow copy copies field values, so object references still point to the same inner objects. A deep copy also copies those inner objects, so changing the copy does not affect the original.

Common Mistakes Freshers Make

  • Definitions without examples. Always add a real-world example and, if possible, a two-line code snippet.
  • Mixing up overloading and overriding. Remember: overloading is the same class with different parameters; overriding is a child class with the same signature.
  • Saying Java has no multiple inheritance at all. It does, through interfaces. It is only blocked for classes.
  • Forgetting Java 8 interface changes. Saying interfaces can only have abstract methods is outdated.
  • Not linking OOP to your project. Interviewers often ask, "Where did you use OOP in your project?" Prepare one concrete answer.

How to Prepare OOPs in One Week

  1. Day 1: Learn the 4-pillar table and explain each pillar out loud with its example.
  2. Days 2–3: Type and run every program in this guide. Change one line and predict the new output before running it.
  3. Day 4: Revise the comparison tables: overloading vs overriding, abstract class vs interface, access modifiers.
  4. Day 5: Practise the tricky questions. Answer each in under 60 seconds.
  5. Day 6: Find where your final-year project uses classes, inheritance or interfaces and prepare a 2-minute explanation.
  6. Day 7: Take a mock interview with a friend, mixing OOP with coding questions from Stack and Queue Interview Questions.

For company-specific practice, see TCS Interview Questions and TCS NQT Interview Questions, where OOP questions appear often in the technical round.

Frequently Asked Questions

What are the 4 pillars of OOPs?

Encapsulation, inheritance, polymorphism and abstraction.

What is the difference between overloading and overriding?

Overloading uses the same method name with different parameters in the same class and is decided at compile time. Overriding redefines a parent method in a child class with the same signature and is decided at runtime.

What is the difference between an abstract class and an interface in Java?

An abstract class can have constructors, any type of fields and both abstract and concrete methods, and a class can extend only one. An interface has only constants, abstract methods plus default and static methods since Java 8, and a class can implement many.

Does Java support multiple inheritance?

Not with classes, to avoid the diamond problem. Java supports multiple inheritance through interfaces.

Which language should I use to answer OOPs questions?

Java is the most common choice in Indian placement interviews, but C++ or Python is fine if you are more comfortable with it. The concepts are the same.

Final Word

Interviewers do not expect freshers to know advanced design patterns. They expect clear answers to the basics: the four pillars, overloading vs overriding, and abstract class vs interface, each with an example. Master the 17 questions and 8 programs above, and you will be well prepared for the OOP part of most technical rounds.

Continue with Stack and Queue Interview Questions, our DSA for Campus Placements roadmap and HR Interview Questions to cover every round.

Crack Your Placement with PlacementBaba

Free practice questions, mock tests and interview tipsStart Practicing — DSA Roadmap →