OOP Interview Questions for Freshers: Inheritance, Polymorphism and Real Code Examples

Move beyond the four OOP definitions. Use small Java examples to explain dynamic dispatch, overloading, access control, abstraction and composition under follow-up questions.

KnowledgeGate Team

Exam prep & CS education

Updated 25 Jul 20266 min read

Every fresher can name the four pillars of OOP. The marks are in the follow-ups: overriding versus overloading, abstract class versus interface, why developers favour composition, and which method runs when a parent reference points to a child object.

Definitions are only the warm-up. The interviewer keeps pulling until you can reason with code or run out of clarity. So trace each Java snippet by hand, predict the output, and be ready to say why it is that and not something else.

OOP pillars: the one-line answer for each

  • Encapsulation: a BankAccount keeps balance private and exposes methods that reject invalid deposits or withdrawals.

  • Abstraction: a PaymentMethod interface exposes pay() while each implementation hides how it talks to its provider.

  • Inheritance: class Circle extends Shape reuses the common shape contract and specialises area calculation.

  • Polymorphism: calling area() through a Shape reference produces different behaviour for a Circle and a Rectangle.

The four ideas work together. Encapsulation protects state, abstraction narrows what callers need to know, inheritance can share a genuine base, and polymorphism lets callers depend on that base without branching on every concrete type. Each pillar gets a longer Java treatment in the Object Oriented Technology guide.

Polymorphism worked example

Use this hierarchy:

class Shape {
    double area(){ return 0; }
    void describe(){ System.out.println("Area = " + area()); }
}
class Circle extends Shape {
    double r;
    Circle(double r){ this.r = r; }
    @Override double area(){ return 3.14 * r * r; }
}
class Rectangle extends Shape {
    double w, h;
    Rectangle(double w, double h){ this.w = w; this.h = h; }
    @Override double area(){ return w * h; }
}

Now execute two calls through the same reference type:

Shape s = new Circle(2);
s.describe();
s = new Rectangle(3, 4);
s.describe();

For the first object, s is declared as Shape, but it points to a Circle. describe() calls area(), and dynamic dispatch selects Circle.area() at runtime. The arithmetic is 3.14 * 2 * 2 = 12.56, so it prints:

Area = 12.56

The reference then points to a Rectangle. Runtime dispatch selects Rectangle.area(). The arithmetic is 3 * 4 = 12.0 as a double, so it prints:

Area = 12.0

The rule is exact: s is declared as Shape but the runtime object type decides which area() runs. This dynamic method dispatch is the most tested OOP idea because it separates a memorised definition from code-level understanding.

Diagram of the Shape inheritance tree: root Shape with area() and describe(); children Circle (overrides area = 3.14 * r * r) and Rectangle (overrides area = w * h). Below it a call-resolution trace: Shape s = new Circle(2); s.describe(), labelled "reference type Shape, object type Circle", dispatching at runtime to Circle.area() = 12.56, and a second arrow for new Rectangle(3,4) reaching Rectangle.area() = 12.0.

Overriding versus overloading

These terms sound similar but describe different decisions:

Question

Overriding

Overloading

What changes?

A subclass supplies a method with the same signature

A method name is reused with a different parameter list

When is it resolved?

Runtime

Compile time

What chooses the method?

Actual object type

Declared argument types and available signatures

Example

Circle.area() replaces Shape.area()

add(int, int) and add(double, double)

Here is the overload:

int add(int a, int b){ return a + b; }
double add(double a, double b){ return a + b; }

add(2, 3) returns integer 5. add(2.0, 3.0) returns double 5.0. The compiler chooses from the argument types, before any runtime object can influence the call.

Private methods are not inherited as overridable methods. Static methods are hidden, not overridden, and selection depends on the reference or class used at the call site.

Abstract class, interface and access modifiers

Use an abstract class when related subclasses need a shared base, common state, constructors or some implemented methods. A class can extend only one class. Use an interface for a capability that otherwise unrelated classes can implement. One class can implement several interfaces, giving multiple inheritance of type without multiple class state.

Java's four access levels are simple when stated by scope:

  • private: accessible only inside the same class.

  • default, or package-private: accessible inside the same package.

  • protected: accessible in the same package and in subclasses under Java's protected-access rules.

  • public: accessible everywhere the class itself is visible.

Do not answer only, "An interface is fully abstract." Modern Java interfaces can have default and static methods. The Java 8 to Java 21 interview features guide explains that evolution in context.

Inheritance versus composition

Inheritance models an is-a relationship: Car extends Vehicle says every Car is a Vehicle. Composition models a has-a relationship: a Car contains an Engine field and delegates engine-related work to it.

The standard advice to favour composition has a practical reason. Inheritance couples a subclass to the parent's behaviour and protected internals. A parent change can silently alter or break subclasses, which is the fragile-base-class problem. With composition, the containing class depends on a smaller collaborator contract and can swap one implementation for another.

Use inheritance only for a true is-a relationship that remains valid wherever the parent type is expected. Use composition otherwise.

Two-panel diagram contrasting inheritance and composition: on the left Car points to Vehicle along an is-a arrow, on the right Car holds an Engine through a has-a link.

OOP interview questions: rapid-fire answers that survive a follow-up

Ten questions come round in almost every fresher interview. One or two sentences is all you get before the follow-up arrives, so keep each answer this tight and let the code do the rest.

  • Can you override a static method? No. A static method declared again in a subclass hides the parent's method. The call is resolved from the reference or class name at compile time, so no runtime dispatch happens.

  • Can a constructor be overloaded or overridden? Overloaded, yes: a class may declare several constructors with different parameter lists. Overridden, no: constructors are not inherited.

  • What does super do? super.method() calls the parent version of an overridden method, and super(...) invokes a parent constructor as the first statement of a child constructor.

  • Can an overriding method reduce visibility? No. An override may widen access, protected to public, but never narrow it, because a caller holding the parent type must still be able to make the call.

  • What is a covariant return type? An override may return a subtype of what the parent method returns, so Shape copy() can be overridden as Circle copy().

  • What does final mean on a class, a method and a field? A final class cannot be extended, a final method cannot be overridden, and a final field must be assigned exactly once and then keeps that value.

  • Why override hashCode() whenever you override equals()? Equal objects must return the same hash code. Break that and HashMap or HashSet will search the wrong bucket and fail to find an object it is already holding.

  • What are upcasting and downcasting? Assigning a Circle to a Shape reference is upcasting and is always safe. Casting back down throws ClassCastException at runtime unless the object really is a Circle, so guard it with instanceof.

  • Can an abstract class have a constructor? Yes. It runs when a concrete subclass is instantiated, and it initialises the shared state that subclass inherits.

  • How do association, aggregation and composition differ? Association is any structural link between two classes. Aggregation is a has-a in which the part can exist on its own, as a Professor can without a particular Department. Composition is a has-a in which the whole owns the part's lifetime, as an Order owns its OrderLine objects.

OOP interview traps and your next step

Freshers repeatedly hit four traps:

  • Overloading is mistaken for overriding: check the parameter list and binding time.

  • Private methods are said to override: they are not inherited for polymorphic replacement.

  • The diamond problem is ignored: Java forbids multiple inheritance of classes, while interfaces provide multiple inheritance of type and rules for resolving default-method conflicts.

  • An overridden method is called from a constructor: dynamic dispatch can enter the subclass method before subclass fields are initialised, producing surprising state.

Interviewers usually move from "define" to "contrast" and then to "write a small class". They may finish by changing the reference or object type and asking you to predict the call. Answer from the Java language rules and reason each step out loud; a memorised slogan collapses at the first follow-up.

The short version is this: runtime object type wins for overridden instance methods, overloading is selected at compile time, and composition is safer unless the relationship is truly is-a. Build and run the examples in the Complete Java course, then keep going through the CS Fundamentals for Placements catalogue. The KnowledgeGate question bank carries over 200 object-oriented programming questions, so practise each answer against a code follow-up.