Object-Oriented Principles and Implementation: Worked Example and Exam Traps
Connect abstraction, encapsulation, inheritance and polymorphism to one Shape program, then trace each method call and verify every result.
KnowledgeGate Team
Exam prep & CS education

Memorising encapsulation, inheritance and polymorphism does not settle which method runs when a base-type reference points to a child object. A base-type reference can point to a child object, connecting private state, an abstract operation, subtype objects, overriding and runtime dispatch. Object Oriented Technology Explained maps the broader class, object, inheritance and interface model. This Shape program uses one area() call to make all four principles cooperate and to support exact output, matching and code-reading practice; related programming tracks sit in the Coding & Skills category.
Object-oriented principles and implementation answer different questions
Term | Question it answers |
|---|---|
Class | What state and behaviour are permitted? |
Object | Which runtime instance, identity and current state are involved? |
Abstraction | What operation may a client request? |
Encapsulation | How is state reached, changed and kept valid? |
Inheritance | Which subtype relationship exists? |
Polymorphism | How can one operation behave according to the actual object? |
Shape.area() is the abstraction in the worked example, and private dimensions support encapsulation. Rectangle and Circle inherit the contract and override the same operation. A Shape[] can therefore call it uniformly while runtime dispatch selects the implementation for each actual object.
Worked example: two shapes and one exact total
abstract class Shape {
public abstract double area();
}
final class Rectangle extends Shape {
private final int width;
private final int height;
Rectangle(int width, int height) {
if (width <= 0 || height <= 0) throw new IllegalArgumentException();
this.width = width;
this.height = height;
}
@Override public double area() {
return width * height;
}
}
final class Circle extends Shape {
private final int radius;
Circle(int radius) {
if (radius <= 0) throw new IllegalArgumentException();
this.radius = radius;
}
@Override public double area() {
return 3.14 * radius * radius;
}
}
public class ShapeTrace {
public static void main(String[] args) {
Shape[] shapes = {new Rectangle(4, 5), new Circle(3)};
double total = 0;
for (Shape s : shapes) total += s.area();
System.out.printf("%.2f%n", total);
}
}Work in source order. The rectangle stores width=4 and height=5, so Rectangle.area() returns 4 * 5 = 20. The circle stores radius=3, so Circle.area() returns 3.14 * 3 * 3 = 28.26. The accumulator is therefore 0 -> 20 -> 48.26, and formatting to two decimal places prints 48.26.
Clients request only area(). Private, constructor-validated dimensions support encapsulation. The concrete subtypes and their overrides supply the calculations.

How overriding becomes runtime dispatch
On the first iteration, s has static type Shape and actual object Rectangle(4,5). Shape permits s.area(); runtime selects Rectangle.area(), returns 20, and changes total to 20.
On the second iteration, the static type remains Shape, but the actual object is Circle(3). Runtime selects Circle.area(), returns 28.26, and changes total to 48.26.
A conceptual method-slot model sends the common area slot to Rectangle.area for a Rectangle and Circle.area for a Circle. Real runtimes may use method tables, type metadata, inline caches or optimisations that remove an apparent lookup. The promise is the selected override and result, not one compulsory layout. Programming Languages Survey gives context for comparing mechanisms.
![Two-row dispatch trace showing shapes[0] as Rectangle with calculation 4 × 5 = 20 and shapes[1] as Circle with calculation 3.14 × 3 × 3 = 28.26, accumulating to 48.26.](https://cdn.knowledgegate.ai/blog-assets/blog_asset_1784735835757_dmvhc7.jpg)
See how the four principles cooperate
Before dispatch, Rectangle(4,5) and Circle(3) validate their arguments, store private state and expose no field mutation. new Circle(0) fails at the boundary instead of creating an invalid shape.
The loop depends only on Shape.area(), so either subtype can occupy either position without changing the loop. Reversing the array changes the path to 0 -> 28.26 -> 48.26, but not the final total.
Abstraction is the stable area() request. Encapsulation protects dimensions and their invariant. Inheritance supplies the “is a Shape” relation. Polymorphism combines the uniform loop with object-specific results. Private fields alone do not create abstraction, and inheritance alone does not ensure safe substitution.
Overloading, overriding and binding are different
Rectangle.area() overrides the inherited Shape.area() contract with the same parameter list. If a utility defines both print(Shape s) and print(Circle c), the pair is overloading because the parameter lists differ. In Java-like overload resolution, selection uses compile-time call types, not the runtime-dispatch rule used for area().
Check three expressions separately:
Shape a = new Rectangle(4,5); a.area()returns20through overriding.Shape b = new Circle(3); b.area()returns28.26through overriding.a.widthis rejected becausewidthis private and absent from theShapeinterface.
Access checking is not method selection. Subtyping also requires a sound relationship: Circle is a Shape fits the contract, while “Report is a Formatter” does not. Model the latter as Report has a Formatter through composition.
How assessments test principles and implementation
Common questions ask you to match code to a principle, predict a base-reference call's output, distinguish overloading from overriding, or repair exposed state or misused inheritance. For a named exam, confirm its current syllabus and pattern in official material.
For
Shape s = new Circle(3), which body runs?Circle.area().What value returns?
3.14 * 3 * 3 = 28.26.If the circle becomes
new Circle(2), its area is3.14 * 2 * 2 = 12.56, so the new total is20 + 12.56 = 32.56.If the original array is reversed, the totals become
28.26and then48.26; the final value is unchanged.
After tracing the Shape program, practise the same reasoning on cohesion, coupling, packaging, candidate objects and OOD activity selection. For every answer, record the principle, the code evidence and the predicted result.
Exam traps and a five-question repair checklist
Class versus object:
Circleis a class;new Circle(3)creates an object.Privacy versus abstraction: private state supports encapsulation;
area()is the client-facing abstraction.Reference type versus actual type: the static type permits the call; the actual type selects the override.
Same name versus overriding: compare the inherited contract and parameter list before deciding.
Reuse versus subtyping: ask whether every child can safely stand where the parent contract is required.
Semantic model versus layout: do not turn a useful dispatch model into a claim about a fixed vtable layout.
For any snippet, ask: What is each reference's static type? What is each object's actual type? Is the method overloaded or overridden? Which state is accessible through the declared interface? What exact value does the selected body return?
Object-oriented principles: the short version and next step
Shape.area() gives abstraction. Private positive dimensions give encapsulation and an invariant. Rectangle and Circle provide subtype inheritance. The Shape[] loop gives polymorphism, while runtime dispatch selects 20 and 28.26 for a total of 48.26.
Now add Triangle(base=6,height=4) with an override that returns 0.5 * 6 * 4 = 12. Appending it changes the accumulator from 48.26 to 60.26. Draw the new actual-type arrow and dispatch row before checking that sum.
Use the Java Course as the structured next step for implementing this model. Then rewrite the example in one familiar language, change exactly one subtype value, and recompute the dispatch trace instead of memorising definitions.
Keep learning
Principles and Implementation MCQs: 7 Solved MCQs Plus 4 OOD Problems
Practise seven bilingual Principles and Implementation MCQs plus four application problems on object-oriented design decisions.
Object-Oriented Programming: Classes, Objects and Inheritance with a Worked Java Example
Trace two Java objects from construction to output, and see how independent state, inheritance and runtime method dispatch work together.
Object-Oriented Programming (OOP): Four Pillars and a Worked Dynamic-Binding Example
Build a precise OOP mental model, then follow two Employee objects through a Java program to see overloading, overriding and runtime dispatch in action.
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.





