<< Chapter < Page Chapter >> Page >

I will begin the discussion of polymorphism with method overloading, which is the simplest of the three. I will cover method overloading in this module and will cover polymorphism based on overridden methods and interfaces in subsequent modules.

Method overloading versus method overriding

Don't confuse method overloading with method overriding .

Java allows you to have two or more method definitions in the same scope with the same name, provided that they have different formal argument lists.

More specifically, here is what Roberts, Heller, and Ernest have to say about overloading methods in their excellent book titled The Complete Java 2 Certification Study Guide :

"A valid overload differs in the number or type of its arguments. Differences in argument names are not significant. A different return type is permitted, but is not sufficient by itself to distinguish an overloading method."

Similarly, as a preview of things to come, here is what they have to say about method overriding:

"A valid override has identical argument types and order, identical return type, and is not less accessible than the original method. The overriding method must not throw any checked exceptions that were not declared for the original method."

You should read these two descriptions carefully and make certain that you recognize the differences.

Compile-time polymorphism

Some authors refer to method overloading as a form of compile-time polymorphism , as distinguished from run-time polymorphism .

This distinction comes from the fact that, with overloaded methods, for each method call, the compiler determines which method (from a group of overloaded methods) will be executed, and this decision is made when the program is compiled. (In contrast, I will tell you later that the determination of which overridden method to execute isn't made until runtime.)

Selection based on the argument list

In practice, the compiler simply examines the types, number, and order of the parameters being passed in an overloaded method call, and selects the overloaded method having a matching formal argument list.

A sample program

I will discuss a sample program named Poly01 to illustrate method overloading. A complete listing of the program can be viewed in Listing 4 near the end of the module.

Within the class and the hierarchy

Method overloading can occur both within a class definition, and vertically within the class inheritance hierarchy. (In other words, an overloaded method can be inherited into a class that defines other overloaded versions of the method.) The program named Poly01 illustrates both aspects of method overloading.

Class B extends class A, which extends Object

Upon examination of the program, you will see that the class named A extends the class named Object . You will also see that the class named B extends the class named A .

The class named Poly01 is a driver class whose main method exercises the methods defined in the classes named A and B .

Once again, this program is not intended to correspond to any particular real-world scenario. Rather, it is a very simple program designed specifically to illustrate method overloading.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask