Java Foundations· 35 min

Object-Oriented Java

Learn classes, objects, constructors, access control, interfaces, inheritance, polymorphism, and composition.

Java interview lesson
Question 2 · Not reviewed

Foundations · object design

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

Object-Oriented Java

Explain it aloud in your own words

Strong answer

Good Java design protects invariants behind a small public API. Encapsulation hides state, abstraction exposes intent, inheritance models a real substitutable relationship, and polymorphism lets callers depend on a stable interface. Composition is usually safer when behavior should be replaceable.

Key points

  • Prefer constructor injection and immutable state where practical.
  • Use interfaces for capabilities, not merely because inheritance is available.
  • Overloading is resolved at compile time; overriding is selected at runtime.
  • Composition reduces coupling and makes testing easier.

Interviewer looks for

Do not only recite the four pillars. Explain a design decision and why composition can be more maintainable than a deep hierarchy.

Java example

1interface Payment {
2  void pay(int cents);
3}
4
5final class Checkout {
6  private final Payment payment;
7  Checkout(Payment payment) { this.payment = payment; }
8}

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Basic

Explain encapsulation and why it matters in Java.

Strong answer

Encapsulation keeps state and the rules that protect it behind a controlled API. It prevents callers from creating invalid state and lets the implementation change without breaking every caller.

Q2Intermediate

What is the difference between method overloading and overriding?

Strong answer

Overloading uses the same method name with different parameter lists and is resolved at compile time. Overriding replaces inherited behavior with the same compatible signature and is selected at runtime.

Q3Intermediate

When would you choose an interface over an abstract class?

Strong answer

Use an interface for a capability or boundary that unrelated classes can implement. Use an abstract class when subclasses genuinely share state or protected implementation, while keeping the hierarchy shallow.

Q4Intermediate

Why is composition often preferred over inheritance?

Strong answer

Composition delegates to replaceable collaborators instead of coupling a class to a superclass implementation. It usually makes behavior easier to test, vary, and evolve.

Q5Intermediate

What does substitutability mean in object-oriented design?

Strong answer

A subtype should work anywhere its base abstraction is expected without violating the caller's assumptions. A subtype that changes required invariants or throws unexpected failures is not a safe substitute.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.