<< Chapter < Page Chapter >> Page >

Many classes, one type

I explained that because different classes can implement the same interface, objects instantiated from different classes can be treated as a common interface type.

Interfaces are critical to Java programming

I suggested that there is little if anything useful that can be done in Java without understanding and using interfaces. In support of this suggestion, I discussed several real-world examples of the use of the Java interface, including the Delegation Event Model andthe Model View Control paradigm.

Another sample program

In this module, I will present another sample program that will take you deeper into the world of polymorphism as implemented using the Java interface.

The sample program that I will discuss in this module will illustrate (in a very basic form) some of the things that you can do with interfaces, along with some of the things that you cannot do with interfaces. In order to write programs that do something worthwhile, you will need to extend the concepts illustrated by this sample program into real-world requirements.

Discussion and sample code

Now, let's take a look at a sample program named Poly06 that is much simpler than any of the real-world examples that you will see in future modules.

This program is designed to be very simple, while still illustrating runtime polymorphism using interfaces, class inheritance, and overridden methods.

You can view a complete listing of the program in Listing 15 near the end of the module.

Same structure as before

Note that this program has the same structure as Poly05 discussed in the previous module . (I strongly recommend that you study the previous module before continuing with this module.) However, unlike the program in the previous module, the methods in this version of the program are not empty. When a method is called in this version, something happens. (Admittedly not much happens. Text is displayed on the computer screen, but that is something.)

The interface definitions

Listing 1 shows the definition of the two interfaces named I1 and I2 .

Listing 1 . Definition of the interfaces named I1 and I2.
interface I1{ public void p();}//end interface I1 //===================================//interface I2 extends I1{ public void q();}//end interface I2

Since the methods declared in an interface are not allowed to have a body, these interface definitions are identical to those shown in the program from the previous module.

The class named A

Similarly, Listing 2 shows the definition of the class named A along with the definition of the method named x , and the overridden method named toString .

Listing 2 . Definition of the class named A.
class A extends Object{public String toString(){ return "toString in A";}//end toString() //---------------------------------//public String x(){return "x in A"; }//end x()//---------------------------------// }//end class A

These two methods were also fully defined in the program from the previous module, so there is no change here either.

The method named B

Listing 3 defines the class named B , which extends A , and implements I2 .

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