Object-oriented principles in Java
31 questions in 11 subtopics.
Encapsulation
What is encapsulation in Java beyond making fields private and adding getters and setters? Give an example where getters and setters still break it.JPA entities in your service have public setters on every field, and business rules are scattered in service classes. What would you change, and what constraints do JPA and frameworks impose?Inheritance and method overriding
List the rules a method must follow to override another in Java, including access, return type, exceptions and static or private methods.What does this print?class Animal {
String name = "animal";
String speak() {
return "...";
}
}
class Dog extends Animal {
String name = "dog";
@Override
String speak() {
return "woof";
}
}
Animal a = new Dog();
System.out.println(a.name + " " + a.speak());
Polymorphism and dynamic dispatch
Replace this type-check chain with polymorphism and explain the benefit:double area(Shape s) {
if (s instanceof Circle c) return Math.PI * c.r() * c.r();
else if (s instanceof Square q) return q.side() * q.side();
throw new IllegalArgumentException();
}
Method overloading resolution
Which method is called in each case?void f(long x) {...}
void f(Integer x) {...}
void f(Object x) {...}
void f(int... x) {...}
f(5);
f(Integer.valueOf(5));
short s = 5;
f(s);
equals(Order other) declared in a class.A List<Integer> remove call deletes the wrong element: list.remove(index) where index is an int, versus list.remove(value) where value is an int. Explain and fix, then say how API design could avoid this.Abstraction
Explain abstraction with an example from the JDK, and how it differs from encapsulation.Teams in your organization create an interface for every class (OrderService and OrderServiceImpl) by default. Is this good abstraction? What guidance would you give?Composition vs inheritance
Why does Effective Java advise favouring composition over inheritance? Explain with theInstrumentedHashSet example and show the composed version.A legacy codebase has a five-level class hierarchy for payment processors with overridden hooks at every level. A new payment type needs behaviour from two branches. How do you approach the redesign?Java frameworks of the 2000s relied heavily on inheritance (base classes for servlets, DAOs, tests), while modern Java favours composition, interfaces and functions. How would you steer a large organization's codebases through this shift over several years?SOLID principles
Explain each SOLID principle in one or two sentences with a Java example.A code reviewer rejects a pull request for violating the open/closed principle because it adds a case to a switch. When is that criticism right and when is it wrong?SOLID was formulated in the 1990s and 2000s for object-oriented codebases. With records, sealed types, lambdas and microservices in modern Java, which parts still matter, which need reinterpretation, and how do you teach them to an organization?Your organization has over-applied design principles: many single-implementation interfaces, deep abstraction layers and factories everywhere, which slow delivery. How do you correct course without swinging to unstructured code?Liskov substitution in practice
Why is the classic Square extends Rectangle example a Liskov substitution violation? Show code that breaks.AReadOnlyList class extends a mutable List implementation and throws UnsupportedOperationException from add. Is that a Liskov violation? How do the JDK's own collections handle this?A platform team replaces a cache client with a new implementation of the same interface. It returns empty results instead of throwing on timeout, and downstream services start serving stale prices. How do you treat this, and how do you prevent it?Covariant return types
What is a covariant return type? Show an example withclone() and explain what the compiler generates.How do covariant return types help with fluent builder APIs in a class hierarchy, and what alternative do recursive generics offer?Method hiding
What does this print, and why?class Parent {
static String who() {
return "parent";
}
String name() {
return "P";
}
}
class Child extends Parent {
static String who() {
return "child";
}
String name() {
return "C";
}
}
Parent p = new Child();
System.out.println(p.who() + " " + p.name());
protected String status; with the same name as the parent's field. Code that reads entity.status through a Parent-typed variable sees null, while methods in the subclass see the value. Explain and fix it.A legacy utility framework in your company relies on subclasses hiding static factory methods of base classes to change behaviour, and teams keep getting the wrong implementation. How would you guide the redesign?Oop pillars overview
Name the four pillars of object-oriented programming and give a one-line Java example of each.A candidate says that Java code using records, sealed interfaces and pattern matching is not object-oriented. How would you respond, and how do the four pillars apply to such a design?Interview loops in your organisation still ask juniors and seniors alike to list the four OOP pillars. How would you change what is assessed about object-oriented design at different levels?No question matches these choices.