1
votes

For a long time I thought polymorphism was defined as one class inheriting from another, such that the inheriting class is of a more narrow type than the super class. Recently however, I've been told that method overloading is also called polymorphism. I am currently under the impression that method overriding and method overloading are respectively called runtime and compile time polymorphism.

My questions are as follows:

  1. Am I right in thinking of method overriding and method overloading as runtime and compile time polymorphism?
  2. Do you need to override anything in order to overload a method?
2
some how edited question is different than what I wanted to ask.user260203

2 Answers

2
votes

The first thing, overriding and overloading, both are different. You are correct, overloading is termed as static or compile time polymorphism. At the compile time itself the Java compiler binds the method calls to respective callers and hence the term. Where as in dynamic or runtime polymorphism, binding is deferred till the execution. We can execute runtime polymorphism using inheritance.

0
votes

Overloading is indeed a form of polymorphism! Albeit one that is not related to Subtyping and one that is not often discussed, or even acknowledged, in Java OOP circles1 - but we can change that!

..polymorphism is the provision of a single interface to entities of different types.

That is, overloading is compile-time Ad hoc (Overloading) Polymorphism at work.

In programming languages, ad hoc polymorphism is a kind of polymorphism in which polymorphic functions can be applied to arguments of different types, because a polymorphic function can denote a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied. [ad hoc polymorphism] is also known as function overloading or operator overloading.

An overloaded method might also participate in run-time Subtype (Inclusion) Polymorphism - realized in Java through method overriding, interfaces, and virtual dispatch.


See these additional questions; the selection is based upon questions with [detailed] answers that [mostly] support the case argued for above..

Other resources; again selected to support the case above..


1The Java Tutorial: Polymorphism makes no reference to overloading polymorphism; but neither does it mention that interfaces are sufficient for (or imply) subtype polymorphism in Java.