Object Oriented Technology Explained: Four Pillars, Worked Java Examples, GATE and Interview Patterns

Build object oriented technology on one running Java example. Learn the four pillars, trace exact outputs, and separate overloading from overriding.

Prashant Jain

KnowledgeGate AI educator

Updated 16 Jul 20265 min read

Most students can recite the four pillars of OOP but freeze when a code snippet asks which method actually runs or why a private field matters. Object oriented technology is not a list of definitions to memorise. It is a way of bundling data with the behaviour that acts on it. This guide builds the complete concept set on one running Java example, with exact outputs, so the ideas connect instead of floating loose.

Class versus object: the distinction everything rests on

A class is a blueprint. An object is a built instance of that class, with its own state. Consider the smallest useful example:

class Circle {
    double radius;
}

Circle c1 = new Circle();
Circle c2 = new Circle();
c1.radius = 7;
c2.radius = 3;

Circle is written once, but c1 and c2 are separate objects. Changing one radius does not change the other. A field holds state, while a method such as area() defines behaviour. Bundling both is OOP's central shift from procedure-led C.

The four pillars are encapsulation, abstraction, inheritance, and polymorphism. We will connect them through Shape, Circle, and Rectangle.

Encapsulation and abstraction: hiding data behind a clean interface

Public fields let any caller write any value. Encapsulation moves that state behind methods that can enforce a valid rule:

class Circle {
    private double radius;

    public void setRadius(double r) {
        if (r > 0) {
            radius = r;
        } else {
            System.out.println("Invalid radius");
        }
    }

    public double getRadius() {
        return radius;
    }
}

c.setRadius(7) stores 7. Calling c.setRadius(-3) then prints Invalid radius and leaves the radius at 7. The object, not merely the private keyword, protects the rule that a radius must be positive.

A caller can use area() without knowing its calculation. Abstraction hides complexity behind an interface, the what. Encapsulation protects data, the how it is guarded. Java's private, default, protected, and public levels control visibility, with private the tightest.

Inheritance: building a class hierarchy without repeated code

Inheritance expresses an is-a relationship. A Circle is a Shape, and a Rectangle is a Shape, so both can share one abstract contract:

abstract class Shape {
    abstract double area();
}

class Circle extends Shape {
    double radius;

    Circle(double r) {
        radius = r;
    }

    double area() {
        return (22.0 / 7) * radius * radius;
    }
}

class Rectangle extends Shape {
    double length, breadth;

    Rectangle(double l, double b) {
        length = l;
        breadth = b;
    }

    double area() {
        return length * breadth;
    }
}

For new Circle(7), the area is (22.0 / 7) * 7 * 7. Cancelling one factor of 7 gives 22 * 7, so the result is 154.0. For new Rectangle(5, 4), the area is 5 * 4 = 20.0.

When new Circle(7) runs, Java invokes the implicit Shape constructor before the Circle constructor body. super() comes first, written explicitly or inserted by the compiler. Since Circle is-a Shape, a Shape reference may point to it.

UML-style class hierarchy with a top box labelled "Shape (abstract)" listing field "none" and method "area(): double"; hollow-triangle extends arrows point up from "Circle", which lists "radius: double", "area() = (22/7)rr", and "radius = 7 -> area = 154.0", and from "Rectangle", which lists "length: double, breadth: double", "area() = length*breadth", and "length = 5, breadth = 4 -> area = 20.0".

Polymorphism: overloading versus overriding and binding time

Overriding gives runtime polymorphism. A subclass supplies the same method signature with its own behaviour:

Shape[] shapes = {
    new Circle(7),
    new Rectangle(5, 4)
};

double total = 0;
for (Shape s : shapes) {
    total += s.area();
}
System.out.println(total);

The reference type is Shape, but the actual object decides which area() runs. The circle contributes 154.0 and the rectangle 20.0, so total = 154.0 + 20.0 = 174.0. This is dynamic, or late, binding.

Overloading is compile-time polymorphism:

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

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

add(2, 3) selects the two-argument method and returns 5; add(2, 3, 4) selects the three-argument method and returns 9. The compiler uses the parameter list. Overriding keeps one signature and resolves from the object at run time. Overloading changes the parameters and resolves at compile time.

Two-column comparison titled "Overloading vs Overriding"; the left column, "Overloading (compile-time / static binding)", lists "same method name, different parameter list", "add(int,int) -> add(2,3)=5", "add(int,int,int) -> add(2,3,4)=9", and "resolved by the compiler"; the right column, "Overriding (run-time / dynamic binding)", lists "same signature in subclass", "Shape s = new Circle(7); s.area() -> 154.0", "Shape s = new Rectangle(5,4); s.area() -> 20.0", and "resolved by the actual object"; a bottom banner reads "Shape[] loop total = 154.0 + 20.0 = 174.0".

Abstract classes versus interfaces: choosing the right contract

An abstract class can contain abstract and concrete methods, fields, and constructors. A class extends only one class. Shape has no general area calculation, so each concrete subclass implements area(). new Shape() is a compile-time error.

An interface is primarily a contract. Modern Java permits default, static, and private interface methods, but a class can implement many interfaces. This provides multiple inheritance of type without multiple class implementation.

Use an abstract class for an is-a family sharing code. Use an interface for a capability across unrelated classes. If Circle implements Comparable<Circle>, it must define compareTo, perhaps ordering circles by radius.

Object oriented technology traps that cost marks

  • Trap: overloading and overriding are the same. Truth: Different parameters mean compile-time overloading. The same signature in a subclass means runtime overriding. add() overloads; area() overrides.

  • Trap: a `Shape` reference always calls `Shape`'s method. Truth: For an overridden instance method, the object decides. Shape s = new Circle(7); s.area() returns 154.0.

  • Trap: encapsulation only means private fields. Truth: It bundles data with its guards. Rejecting -3 while retaining 7 is the protection.

  • Trap: abstraction and encapsulation are identical. Truth: Abstraction hides complexity. Encapsulation hides and protects state.

  • Trap: an abstract class can be instantiated. Truth: new Shape() does not compile. Instantiate a concrete subclass instead.

  • Trap: Java supports multiple inheritance of classes. Truth: A class has one direct superclass but may implement multiple interfaces.

GATE and interviews: how object oriented technology is tested

Predict-the-output questions give a base reference pointing to a subclass and ask which method executes. This tests dynamic dispatch. Shape s = new Circle(7); s.area() produces 154.0 because the object is a Circle.

Overload-resolution questions ask which signature the compiler selects. Conceptual questions compare abstraction with encapsulation or abstract classes with interfaces. The parent constructor runs first through super(). Constructors cannot be overridden because they are not inherited. Java has no virtual keyword, but overridable instance methods use virtual dispatch.

Trace any snippet with two questions in order: what is the actual object type, and is the call overloaded or overridden? Use the Coding and CS Fundamentals hub for more subject notes. Once the concepts are firm, Data Structures MCQs gives you the same practice-question style on another core area.

The short version and your next step

Object oriented technology bundles state with behaviour. Its pillars are encapsulation, abstraction, inheritance, and polymorphism. Overloading is selected at compile time from parameters; overriding is selected at run time from the object.

The Complete Java Course builds these ideas from first program to class hierarchies. For readiness across C, C++, Java, and Python, Coding For Placements covers OOP in interview format. The Placements and Off-Campus hub connects it to broader interview work.

Now recreate the Shape[] example in a blank editor. Predict 174.0 before running it. If you can explain why each object selects a different area() method, you understand dynamic dispatch rather than merely recognising its definition.

Discussion