<< Chapter < Page Chapter >> Page >

New method

The method named x is newly defined in the class named A . (The method named x is not inherited into the class named A , because the class named Object does not define a method named x .)

The class named B

Listing 3 contains material that is new to this module.

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

Implementing an interface

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

As you already know, a class in Java can extend only one other class. However, a Java class can implement any number of interfaces. (Multiple inheritance is allowed with interfaces.)

Similar to an abstract class

An interface is similar, but not identical, to an abstract class. (An abstract class may contain abstract methods or concrete methods, or a combination of the two while all of the methods in an interface are implicitly abstract.)

Restrictions

An abstract class cannot be instantiated. Thus, an abstract class is only useful when it is extended by another class.

An interface also cannot be instantiated.

Implicitly abstract methods

As mentioned above, all methods declared in an interface are implicitly abstract, but that is not true for an abstract class. An abstract class can also contain fully-defined (concrete) methods. Regardless, an abstract class cannot be instantiated.

A totally abstract class

At the risk of offending the purists, I will coin a new term here and say that an interface is similar to a totally abstract class (one that contains only abstract method declarations and final variables) .

To a first degree of approximation then, we might say that the class named B is not only a subclass of the class named A , it is also a subclass of the totally abstract class named I2 . (This is pretty far out with respect to terminology, so to avoid being embarrassed, you probably shouldn't repeat it to anyone else.)

Since I2 extends I1 , we might also say that the class named B is a subclass of the totally abstract class named I1 .

A different kind of thinking

With this kind of thinking, we have suddenly make it possible for Java classes to support multiple inheritance , with the stipulation that all but one of the inherited classes must be totally abstract classes .

Be very careful with this way of thinking

However, we need to be very careful with this kind of thinking. While it may help some students to understand the role of interfaces in Java, there are probably some hidden dangers lurking here.

Back to the safety zone

The safest course of action is to simply say that the class named B :

  • Extends the class named A
  • Implements the interface named I2 directly
  • Implements the interface named I1 through inheritance

Java does not support multiple inheritance, but it does allow you to extend one class and implement any number of interfaces.

The cardinal rule regarding interface implementation

The cardinal rule in implementing interfaces is:

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