KatInterview

Java Dispatch Rules and Choosing Between Monolith and Microservices

System DesignModel answer
MediumModel answerSystem Designjavapolymorphismdynamic-dispatch

Last updated

Explain how Java decides which method runs (overriding versus overloading) and weigh a single deployable application against a set of independent services, including how to migrate.

This is a two-part conceptual question. Answer both parts in prose, with a short code sample where it helps.

Part 1: How Java chooses which method to run

Define polymorphism and describe the forms it takes in Java. Explain how the compiler and the JVM together decide which method body executes for a given call. Include a brief code example in which a reference of a parent class or interface type holds objects of different subclasses.

Your answer should address:

  • Overriding and dynamic dispatch at runtime, compared with overload resolution at compile time.
  • Which members do not take part in runtime dispatch (static methods, private methods, final methods and fields) and the reason for each.
  • The design benefit of programming against a supertype.

Part 2: Monolith or microservices

Compare a monolithic architecture with a microservices architecture. Say when you would pick each one and what it costs to move from the first to the second.

Your answer should address:

  • Deployment, scaling, fault isolation and freedom to choose technologies.
  • The price of distribution: network failures, keeping data consistent across services, observability and operational load.
  • A recommendation for a concrete scenario of your choosing, plus a step-by-step migration path.

You may state your assumptions about the product, team size or stage up front.

Hints

Hint 1

For Part 1, separate what is decided at compile time (which method signature is selected, based on the static types of the arguments) from what is decided at run time (which implementation of that signature runs, based on the actual object type).

Hint 2

Ask of each member kind whether it is looked up through the object's runtime class. Static methods and fields are resolved by the declared type, private methods are not visible to subclasses, and final methods are forbidden from being overridden.

Hint 3

For Part 2, do not declare a winner in the abstract. Tie your recommendation to team size, how independent the business domains are, and how uneven the load is, then describe a gradual extraction instead of a rewrite.