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 15 Jul 20264 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. These small Java examples give you something concrete to trace and explain.

The four OOP pillars in real code

  • 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.

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 teaching point 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.

the Shape inheritance tree. Root Shape with area() and describe(); children Circle (overrides area = 3.14rr) and Rectangle (overrides area = w*h). A call-resolution arrow: Shape s = new Circle(2); s.describe() labelled "reference type Shape, object type Circle" dispatching at runtime to Circle.area() = 12.56; a second arrow for new Rectangle(3,4) to 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.

is-a vs has-a side by side. Left: inheritance, Vehicle with an "is-a" arrow up from Car (Car extends Vehicle). Right: composition, Car with a diamond "has-a" link to Engine (Car holds an Engine field). A caption: "favour composition unless it is a true is-a relationship".

Traps, interview probes 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. OOP has no single interview authority, so rely on the Java language rules and consistent reasoning, not a list of slogans.

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 continue through the Coding Skills catalogue. KnowledgeGate's bank has more than 200 published OOPS questions, so practise each answer against a code follow-up.