<< Chapter < Page Chapter >> Page >

If a class implements an interface, it must provide a concrete definition for all the methods declared by that interface, and all the methods inherited by that interface. Otherwise, the class must be declared abstract and the definitions must be provided by a class that extends the abstract class.

The cardinal rule regarding class inheritance

A similar rule exists for defining classes that inherit abstract methods from other classes:

If a class inherits one or more abstract methods from its superclasses, it must provide concrete definitions for all the inherited abstract methods. Otherwise, the class must be declared abstract and theconcrete definitions must be provided by a class that extends the abstract class.

What does that mean in this case?

In this case, this means that the class named B must provide concrete definitions for the methods named p and q , because:

  • The class named B implements the interface named I2 .
  • The method named q is declared in the interface named I2 .
  • The interface named I2 extends the interface named I1 .
  • The method named p is declared in the interface named I1 .

As in method overriding, the signature of the concrete method in the defining class must match the signature of the method as it is declared in the interface.

Class B satisfies the cardinal rule

As you can see from Listing 3 , the class named B does provide concrete (but empty) definitions of the methods named p and q .

(As mentioned earlier, I made the methods empty in this program for simplicity. However, it is not uncommon to define empty methods in classes that implement interfaces that declare a large number of methods, such as the MouseListener interface. See my tutorials on event-driven programming here for examples.)

The class named C

Listing 4 defines a class named C , which extends Object , and also implements I2 . As in the case of the class named B , this class must, and does, provide concrete (but empty) definitions for the methods named p and q .

Listing 4 . Definition of the class named C.
class C extends Object implements I2{ public void p(){}//end p() //---------------------------------//public void q(){}//end q(); //---------------------------------//}//end class B

A driver class

Finally, the driver class named Poly05 shown in Listing 5 defines an empty main method.

Listing 5 . The driver class named Poly05.
public class Poly05{ public static void main(String[]args){ }//end main}//end class Poly05

Doesn't do anything

As mentioned earlier, the purpose of this program is solely to illustrate an inheritance and interface structure. This program can be compiled and executed, but it doesn't do anything useful.

A new relationship

At this point, it might be useful for you to sketch out the structure in a simple hierarchy diagram.

If you do, you will see that implementation of the interface named I2 by the classes named B and C , has created a relationship between those two classes that is totally independent of the normal class hierarchical relationship.

What is the new relationship?

By declaring that both of these classes implement the same interface named I2 , we are guaranteeing that an object of either class will contain concrete definitions of the two methods declared in the interfaces named I2 and I1 .

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