Sitemap
Javarevisited

A humble place to learn Java and Programming better.

on

Member-only story

In Method Overriding, How Does Java Determine Which Overridden Method to Execute?

4 min readApr 15, 2024

--

One of the most prominent features in Java that we’re familiar with as Java developers is method overriding, which is a way to achieve run-time Polymorphism. When a class extends a another, it overrides all methods in the extended class, providing its own implementation of each method. Upon running the program, the overridden version of the method in the subclass is called. How is it that the subclass method and not the original method that is executed?

In this short article, we’re going to dive into the simple yet important concept of Dynamic Method Dispatch which is closely related to run-time polymorphism as well as inheritence.

Run-time (Dynamic) Polymorphism

Before we answer the question in the introduction, let’s first understand what Polymorphism is and what do we mean by run-time or dynamic Polymorphism.

Polymorphism is one of the most fundamental concepts in Object Oriented Programming and is used in the context of Inheritance where subclasses override methods from the superclass or interface and define different implementations. This allows objects of subclass types to reuse the same method but with different behavior. So polymorphism makes code more flexible, reusable and easier to maintain.

Javarevisited
Javarevisited

Published in Javarevisited

A humble place to learn Java and Programming better.

Firas Ahmed
Firas Ahmed

Written by Firas Ahmed

Hey, I'm a backend developer and I love to share what I learn with the community.

No responses yet