<< Chapter < Page Chapter >> Page >

Furthermore, we are guaranteeing that objects instantiated from the two classes can be treated as the common type I2 .

( Important : references to any objects instantiated from classes that implement I2 , can be stored in reference variables of the type I2 , and any of the interface methods can be called on those references.)

We know the user interface

The signatures of the interface methods in the two classes must match the signatures declared in the interfaces.

This means that if we have access to the documentation for the interfaces, we also know the signatures of the interface methods for objects instantiated from any class that implements the interfaces.

Different behavior

However, and this is extremely important, the behavior of the interface methods as defined in the class named B may be (and often will be) entirely different from the behavior of the interface methods having the same signatures as defined in the class named C .

Possibly the most powerful concept in Java

This is possibly the most powerful (and most difficult) concept embodied in the Java programming language.

If you don't understand interfaces ...

I usually tell my students several times each semester that if they don't understand interfaces, they don't really understand Java.

It is unlikely that you will ever be successful as a Java programmer without an understanding of interfaces.

There are very few worthwhile programs that can be written in Java without an understanding of interfaces.

The core aspect

So, what is the core aspect of this concept that is so powerful?

I told you earlier that each interface definition constitutes a new type. As a result, a reference to any object instantiated from any class that implements a given interface can be treated as the type of the interface.

So what!

When a reference to an object is treated as an interface type, any method declared in, or inherited into that interface can be called on the reference to the object.

However, the behavior of the method when called on references to different objects of the same interface type may be very different. In the current jargon, the behavior is appropriate for the object on which it is called .

One object, many types

Furthermore, because a single class can implement any number of different interfaces, a single object instantiated from a given class can be treated as any of the interface types implemented by the class from which it is instantiated. Therefore, a single object in Java can be treated as many different types.

(However, when an object is treated as an interface type, only those methods declared in that interface can be called on the object. To call other methods on the object, it necessary to cast the object's reference to a different type.)

Treating different types of objects as a common type

All of this also makes it possible to treat objects instantiated from widely differing classes as the same type, provided that all of those classes implement the same interface.

Important : When an interface method is called on one of the objects using the reference of the interface type, the behavior of the method will be as defined by the author of the specific class that implemented the interface. The behavior of the method will often be different for different objects instantiated from different classes that implement the same interface.

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