<< Chapter < Page Chapter >> Page >

Will discuss in fragments

As is my usual approach, I will discuss this program in fragments. The code in Listing 1 defines the class named A , which explicitly extends Object .

Listing 1 . Definition of the class named A.
class A extends Object{ public void m(){System.out.println("m()"); }//end method m()}//end class A

Redundant code

Explicitly extending Object is not required (but it also doesn't hurt anything to do it) .

By default, the class named A would extend the class named Object automatically, unless the class named A explicitly extends some other class.

The method named m()

The code in Listing 1 defines a method named m() . Note that this version of the method has an empty argument list (it doesn't receive any parameters when it is executed) . The behavior of the method is simply to display a message indicating that it has been called.

The class named B

Listing 2 contains the definition for the class named B . The class named B extends the class named A , and inherits the method named m defined in the class named A .

Listing 2 . Definition of the class named B.
class B extends A{ public void m(int x){System.out.println("m(int x)"); }//end method m(int x)//---------------------------------//public void m(String y){ System.out.println("m(String y)");}//end method m(String y) }//end class B

Overloaded methods

In addition to the inherited method named m , the class named B defines two overloaded versions of the method named m :

  • m(int x)
  • m(String y)

(Note that each of these two versions of the method receives a single parameter, and the type of the parameter is different in each case.)

As with the version of the method having the same name defined in the class named A , the behavior of each of these two methods is to display a message indicating that it has been called.

The driver class

Listing 3 contains the definition of the driver class named Poly01 .

Listing 3 . Definition of the driver class named Poly01.
public class Poly01{ public static void main(String[]args){ B var = new B();var.m(); var.m(3);var.m("String"); }//end main}//end class Poly01

Call all three overloaded methods

The code in the main method

  • Instantiates a new object of the class named B , and
  • Successively calls each of the three overloaded versions of the method named m on the reference to that object.

One version is inherited

The overloaded version of the method named m , defined in the class named A , is inherited into the class named B . Therefore, it can be called on a reference to an object instantiated from the class named B .

Two versions defined in class B

The other two versions of the method named m are defined in the class named B . Thus, they also can be called on a reference to an object instantiated from the class named B .

The output

As you would expect, the output produced by sending messages to the object asking it to execute each of the three overloaded versions of the method named m is:

m() m(int x)m(String y)

Note that the values of the parameters passed to the methods do not appear in the output. Rather, in this simple example, the parameters are used solely to make it possible for the compiler to select the correct version of the overloaded method to execute.

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