<< Chapter < Page Chapter >> Page >
class A extends Base implements X{ public void inherMethod(){System.out.print( " A-inherMethod ");}//end inherMethod()public void intfcMethodX(){ System.out.print("A-intfcMethodX "); }//end intfcMethodX()}//end class A class B extends Base implements X{public void inherMethod(){ System.out.print(" B-inherMethod "); }//end inherMethod()public void intfcMethodX(){System.out.print("B-intfcMethodX "); }//end intfcMethodX()}//end class B

Concrete definitions of the interface method

As before, these methods provide concrete definitions of the method named intfcMethodX , which is declared in the interface named X .

An array of references of type Object

The interesting portion of this program begins in the following fragment, which instantiates and populates a two-element array object (container) of type Object . (In the sense of this discussion, an array object is a container, albeit a very simple one.)

void doIt(){ Object[]myArray = new Object[2];myArray[0] = new A();myArray[1] = new B();

Store object references of type A and B as type Object

Because the container is declared to be of type Object , references to objects instantiated from any class can be stored in the container. The code in theabove fragment instantiates two objects, (one of class A and the other of class B ), and stores the two object's references in the container.

Cannot call interface method as type Object

The code in the for loop in the next fragment attempts to call the method named intfcMethodX on each of the two objects whose references are stored in the elements of the array.

for(int i=0;i<myArray.length;i++){ myArray[i].intfcMethodX(); }//end for loopSystem.out.println(""); }//end doIt()

This produces the following compiler error under JDK 1.3:

Ap136.java:24: cannot resolve symbol symbol : method intfcMethodX ()location: class java.lang.ObjectmyArray[i].intfcMethodX();

What methods can you call as type Object?

It is allowable to store the reference to an object instantiated from any class in a container of the type Object . However, the only methods that can be directly called (without a cast and not using generics) on that reference are the following eleven methods. These methods are defined in the class named Object :

  • clone ()
  • equals(Object obj)
  • finalize()
  • getClass()
  • hashCode()
  • notify()
  • notifyAll()
  • toString()
  • wait()
  • wait(long timeout)
  • wait(long timeout,int nanos)

Overridden methods

Some, (but not all) , of the methods in the above list are defined with default behavior in the Object class, and are meant to be overridden in new classes that you define. This includes the methods named equals and toString .

Some of the methods in the above list, such as getClass , are simply utility methods, which are not meant to be overridden.

Polymorphic behavior applies

If you call one of these methods on an object's reference (being stored as type Object) , polymorphic behavior will apply. The version of the method overridden in, or inherited into, the class from which the object wasinstantiated will be identified and executed.

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