<< Chapter < Page Chapter >> Page >

Interfaces are critical to Java programming

I will suggest that there is little if anything useful that can be done in Java without understanding and using interfaces.

In support of this suggestion, I will discuss several real-world examples of the use of the Java interface, including the Delegation Event Model, the Model View Control paradigm, and iterators in Java data structures.

Discussion and sample code

Listing 6 near the end of the module contains a very simple program named Poly05 .

The purpose of this program is to illustrate polymorphic behavior using interfaces in addition to class inheritance.

Designed to illustrate structure

This is a skeleton program designed solely to illustrate the inheritance and interface implementation structure in as simple a program as possible. (I will put some meat on this skeleton using another program in the next module.)

Empty methods

Except for the two methods that return type String , all of the methods in the program are empty. (Methods that return type String cannot be empty. They must contain a return statement in order to compile successfully.)

Interface definitions

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

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

Similar but different

An interface definition is similar to a class definition. However, there are some very important differences.

No single hierarchy

To begin with, unlike the case with classes, there is no single interface hierarchy. Also, multiple inheritance is allowed when extending interfaces.

A new interface can extend none, one, or more existing interfaces. In Listing 1 , I2 extends I1 , but I1 doesn't extend any other interface (and unlike classes, an interface doesn't automatically extend another interface by default) .

Two kinds of members allowed

Only two kinds of members are allowed in an interface definition:

  • Methods, which are implicitly abstract
  • Variables, which are implicitly constant (final)

Each of the interfaces in Listing 1 declares an implicitly abstract method (an abstract method does not have a body) .

Neither of the interfaces in Listing 1 declares any variables (they aren't needed for the purpose of this module) .

A new data type

I told you earlier that when you define a new class, you cause a new data type to become available to your program. The same is true of an interface definition. Each interface definition constitutes a new type.

The class named A

Listing 2 defines a very simple class named A , which in turn defines two methods named toString and x .

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

Overridden toString

The method named toString in Listing 2 is actually an overridden version of the method having the same name that is defined in the class named Object . (Recall that a previous module made heavy use of overridden versions of the toString method.)

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