Skip to content Skip to navigation

OpenStax-CNX

You are here: Home » Content » Ap0120: Self-assessment, Interfaces and polymorphic behavior

Navigation

Recently Viewed

This feature requires Javascript to be enabled.
 

Ap0120: Self-assessment, Interfaces and polymorphic behavior

Module by: R.G. (Dick) Baldwin. E-mail the author

Summary: Part of a self-assessment test designed to help you determine how much you know about interfaces and polymorphic behavior in Java.

Preface

This module is part of a self-assessment test designed to help you determine how much you know about object-oriented programming using Java.

The test consists of a series of questions with answers and explanations of the answers.

The questions and the answers are connected by hyperlinks to make it easy for you to navigate from the question to the answer and back.

I recommend that you open another copy of this document in a separate browser window and use the links to under Listings to easily find and view the listings while you are reading about them.

Questions

Question 1 .

What output is produced by the program shown in Listing 1 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. Base A-intfcMethod
  • D. None of the above.
1
Listing 1: Listing for Question 1.
public class Ap131{
  public static void main(
                        String args[]){
    new Worker().doIt();
  }//end main()
}//end class Ap131

class Worker{
  void doIt(){
    Base myVar1 = new Base();
    myVar1.inherMethod();
    X myVar2 = new A();
    myVar2.intfcMethod();

    System.out.println("");
  }//end doIt()
}// end class Worker

class Base{
  public void inherMethod(){
    System.out.print("Base ");
  }//end inherMethod()
}//end class Base

class A extends Base{
  public void inherMethod(){
    System.out.print(
                    " A-inherMethod ");
  }//end inherMethod()
    
  public void intfcMethod(){
    System.out.print("A-intfcMethod ");
  }//end intfcMethod()
}//end class A

interface X{
  public void intfcMethod();
}//end X

Answer and Explanation

Question 2

What output is produced by the program shown in Listing 2 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. A-inherMethod A-intfcMethod
  • D. None of the above.
2
Listing 2: Listing for Question 2.
public class Ap132{
  public static void main(
                        String args[]){
    new Worker().doIt();
  }//end main()
}//end class Ap132

class Worker{
  void doIt(){
    Base myVar1 = new Base();
    myVar1.inherMethod();
    Base myVar2 = new A();
    myVar2.intfcMethod();

    System.out.println("");
  }//end doIt()
}// end class Worker

class Base{
  public void inherMethod(){
    System.out.print("Base ");
  }//end inherMethod()
}//end class Base

class A extends Base implements X{
  public void inherMethod(){
    System.out.print(
                    " A-inherMethod ");
  }//end inherMethod()
    
  public void intfcMethod(){
    System.out.print("A-intfcMethod ");
  }//end intfcMethod()
}//end class A

interface X{
  public void intfcMethod();
}//end X

Answer and Explanation

Question 3

What output is produced by the program shown in Listing 3 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. Base A-intfcMethod
  • D. None of the above.
3
Listing 3: Listing for Question 3.
public class Ap133{
  public static void main(
                        String args[]){
    new Worker().doIt();
  }//end main()
}//end class Ap133

class Worker{
  void doIt(){
    Base myVar1 = new Base();
    myVar1.inherMethod();
    A myVar2 = new A();
    myVar2.intfcMethod();

    System.out.println("");
  }//end doIt()
}// end class Worker

class Base{
  public void inherMethod(){
    System.out.print("Base ");
  }//end inherMethod()
}//end class Base

class A extends Base implements X{
  public void inherMethod(){
    System.out.print(
                    " A-inherMethod ");
  }//end inherMethod()
    
  public void intfcMethod(){
    System.out.print("A-intfcMethod ");
  }//end intfcMethod()
}//end class A

interface X{
  public void intfcMethod();
}//end X

Answer and Explanation

Question 4

What output is produced by the program shown in Listing 4 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. Base A-intfcMethod
  • D. None of the above.
4
Listing 4: Listing for Question 4.
public class Ap134{
  public static void main(
                        String args[]){
    new Worker().doIt();
  }//end main()
}//end class Ap134

class Worker{
  void doIt(){
    Base myVar1 = new Base();
    myVar1.inherMethod();
    X myVar2 = new A();
    myVar2.intfcMethod();

    System.out.println("");
  }//end doIt()
}// end class Worker

class Base{
  public void inherMethod(){
    System.out.print("Base ");
  }//end inherMethod()
}//end class Base

class A extends Base implements X{
  public void inherMethod(){
    System.out.print(
                    " A-inherMethod ");
  }//end inherMethod()
    
  public void intfcMethod(){
    System.out.print("A-intfcMethod ");
  }//end intfcMethod()
}//end class A

interface X{
  public void intfcMethod();
}//end X

Answer and Explanation

Question 5

What output is produced by the program shown in Listing 5 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. A-intfcMethodX B-intfcMethodX
  • D. None of the above.
5
Listing 5: Listing for Question 5.
public class Ap135{
  public static void main(
                        String args[]){
    new Worker().doIt();
  }//end main()
}//end class Ap135

class Worker{
  void doIt(){
    X myVar1 = new A();
    myVar1.intfcMethodX();
    X myVar2 = new B();
    myVar2.intfcMethodX();

    System.out.println("");
  }//end doIt()
}// end class Worker

class Base{
  public void inherMethod(){
    System.out.print("Base ");
  }//end inherMethod()
}//end class Base

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

interface X{
  public void intfcMethodX();
}//end X

Answer and Explanation

Question 6

What output is produced by the program shown in Listing 6 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. A-intfcMethodX B-intfcMethodX
  • D. None of the above.
6
Listing 6: Listing for Question 6.
public class Ap136{
  public static void main(
                        String args[]){
    new Worker().doIt();
  }//end main()
}//end class Ap136

class Worker{
  void doIt(){
    Object[] myArray = new Object[2];
    myArray[0] = new A();
    myArray[1] = new B();
    
    for(int i=0;i<myArray.length;i++){
      myArray[i].intfcMethodX();
    }//end for loop

    System.out.println("");
  }//end doIt()
}// end class Worker

class Base{
  public void inherMethod(){
    System.out.print("Base ");
  }//end inherMethod()
}//end class Base

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

interface X{
  public void intfcMethodX();
}//end X

Answer and Explanation

Question 7

What output is produced by the program shown in Listing 7 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. A-intfcMethodX B-intfcMethodX
  • D. None of the above.
7
Listing 7: Listing for Question 7.
public class Ap137{
  public static void main(
                        String args[]){
    new Worker().doIt();
  }//end main()
}//end class Ap137

class Worker{
  void doIt(){
    Object[] myArray = new Object[2];
    myArray[0] = new A();
    myArray[1] = new B();
    
    for(int i=0;i<myArray.length;i++){
      ((X)myArray[i]).intfcMethodX();
    }//end for loop

    System.out.println("");
  }//end doIt()
}// end class Worker

class Base{
  public void inherMethod(){
    System.out.print("Base ");
  }//end inherMethod()
}//end class Base

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

interface X{
  public void intfcMethodX();
}//end X

Answer and Explanation

Question 8

What output is produced by the program shown in Listing 8 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. A-intfcMethodX B-intfcMethodX
  • D. None of the above.
8
Listing 8: Listing for Question 8.
public class Ap138{
  public static void main(
                        String args[]){
    new Worker().doIt();
  }//end main()
}//end class Ap138

class Worker{
  void doIt(){
    X[] myArray = new X[2];
    myArray[0] = new A();
    myArray[1] = new B();
    
    for(int i=0;i<myArray.length;i++){
      myArray[i].intfcMethodX();
    }//end for loop

    System.out.println("");
  }//end doIt()
}// end class Worker

class Base{
  public void inherMethod(){
    System.out.print("Base ");
  }//end inherMethod()
}//end class Base

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

interface X{
  public void intfcMethodX();
}//end X

Answer and Explanation

Question 9

What output is produced by the program shown in Listing 9 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. Base A B
  • D. None of the above.
9
Listing 9: Listing for Question 9.
public class Ap139{
  public static void main(
                        String args[]){
    new Worker().doIt();
  }//end main()
}//end class Ap139

class Worker{
  void doIt(){
    Base myVar = new Base();
    myVar.test();
    myVar = new A();
    myVar.test();
    myVar = new B();
    myVar.test();
    System.out.println("");
  }//end doIt()
}// end class Worker

class Base{
  public void test(){
    System.out.print("Base ");
  }//end test()
}//end class Base

class A extends Base implements X,Y{
  public void test(){
    System.out.print("A ");
  }//end test()
}//end class A

class B extends Base implements X,Y{
  public void test(){
    System.out.print("B ");
  }//end test()
}//end class B

interface X{
  public void test();
}//end X

interface Y{
  public void test();
}//end Y

Answer and Explanation

Question 10

What output is produced by the program shown in Listing 10 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. Base A B B
  • D. None of the above.
10
Listing 10: Listing for Question 10.
public class Ap140{
  public static void main(
                        String args[]){
        new Worker().doIt();
  }//end main()
}//end class Ap140

class Worker{
  void doIt(){
    Base myVar1 = new Base();
    myVar1.test();
    myVar1 = new A();
    myVar1.test();
    myVar1 = new B();
    myVar1.test();

    X myVar2 = (X)myVar1;
    myVar2.test();

    System.out.println("");
  }//end doIt()
}// end class Worker

class Base{
  public void test(){
    System.out.print("Base ");
  }//end test()
}//end class Base

class A extends Base implements X,Y{
  public void test(){
    System.out.print("A ");
  }//end test()
}//end class A

class B extends Base implements X,Y{
  public void test(){
    System.out.print("B ");
  }//end test()
}//end class B

interface X{
  public void test();
}//end X

interface Y{
  public void test();
}//end Y

Answer and Explanation

Listings

I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the listings while you are reading about them.

Miscellaneous

This section contains a variety of miscellaneous information.

Note:

Housekeeping material
  • Module name: Ap0120: Self-assessment, Interfaces and polymorphic behavior
  • File: Ap0120.htm
  • Originally published: 2004
  • Published at cnx.org: 12/08/12
  • Revised: 01/02/13

Note:

Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

Answers

Answer 10

C. Base A B B

Explanation 10

Expansion of the program from Question 9

The class and interface definitions for the classes and interfaces named Base , A , B , X , and Y are the same as in Question 9 .

Call the test method differently

However, the call of the method named test in the object instantiated from the class named B is somewhat different. The difference is identified by the code in the following fragment.

Note:
  void doIt(){
    Base myVar1 = new Base();
    myVar1.test();
    myVar1 = new A();
    myVar1.test();
    myVar1 = new B();
    myVar1.test();

    X myVar2 = (X)myVar1;
    myVar2.test();

    System.out.println("");
  }//end doIt()

Calling test method on Base-type reference

In Question 9 , and in the above code fragment as well, the method named test was called on each of the objects using a reference stored in a reference variable of type Base .

Calling the overridden version of test method

This might be thought of as calling the overridden version of the method, through polymorphism, without regard for anything having to do with the interfaces.

Calling test method on interface-type reference

Then the code shown above calls the same method named test on one of the same objects using a reference variable of the interface type X .

Only one test method in each object

Keep in mind that each object defines only one method named test . This single method serves the dual purpose of overriding the method having the same signature from the superclass, and implementing a method with the same signature declared in each of the interfaces.

Implementing the interface method

Perhaps when the same method is called using a reference variable of the interface type, it might be thought of as implementing the interface method rather than overriding the method defined in the superclass. You can be the judge of that.

The same method is called regardless of reference type

In any event, in this program, the same method is called whether it is called using a reference variable of the superclass type, or using a reference variable of the interface type.

Illustrates the behavior of signature collisions

The purpose of this and Question 9 is not necessarily to illustrate a useful inheritance and implementation construct. Rather, these two questions are intended to illustrate the behavior of Java for the case of duplicated superclass and interface method signatures.

Back to Question 10

Answer 9

C. Base A B

Explanation 9

A question regarding signature collisions

The question often arises in my classroom as to what will happen if a class inherits a method with a given signature and also implements one or more interfaces that declare a method with an identical signature.

The answer

The answer is that nothing bad happens, as long as the class provides a concrete definition for a method having that signature.

Only one method definition is allowed

Of course, only one definition can be provided for any given method signature, so that definition must satisfy the needs of overriding the inherited method as well as the needs of implementing the interfaces.

An example of signature collisions

The following fragment defines a class named Base that defines a method named test . The code also defines two interfaces named X and Y , each of which declares a method named test with an identical signature.

Note:
class Base{
  public void test(){
    System.out.print("Base ");
  }//end test()
}//end class Base

interface X{
  public void test();
}//end X

interface Y{
  public void test();
}//end Y

class A extends Base implements X,Y{
  public void test(){
    System.out.print("A ");
  }//end test()
}//end class A

Classes A and B extend Base and implement X and Y

The code in the following fragment defines two classes, named A and B , each of which extends Base , and each of which implements both interfaces X and Y . Each class provides a concrete definition for the method named test , with each class providing a different definition.

Note:
class A extends Base implements X,Y{
  public void test(){
    System.out.print("A ");
  }//end test()
}//end class A

class B extends Base implements X,Y{
  public void test(){
    System.out.print("B ");
  }//end test()
}//end class B

Override inherited method and define interface method

Each of the methods named test in the above fragment serves not only to override the method inherited from the class named Base , but also to satisfy the requirement to define the methods declared in the implemented interfaces named X and Y . (This can also be thought of as overriding an inherited abstract method from an interface.)

Store object's references as type Base and call test method

Finally, the code in the following fragment declares a reference variable of the type Base . Objects respectively of the classes Base , A , and B are instantiated and stored in the reference variable. Then the method named test is called on each of the references in turn.

Note:
  void doIt(){
    Base myVar = new Base();
    myVar.test();
    myVar = new A();
    myVar.test();
    myVar = new B();
    myVar.test();
    System.out.println("");
  }//end doIt()
}// end class Worker

As you probably expected, this causes the following text to appear on the screen:

Note:
Base A B

Back to Question 9

Answer 8

C. A-intfcMethodX B-intfcMethodX

Explanation 8

Similar to previous two programs

This program is very similar to the programs in Question 6 and Question 7 . The program is Question 6 exposed a specific type mismatch problem. The program in Question 7 provided one solution to the problem.

A different solution

The following fragment illustrates a different solution to the problem.

Note:
  void doIt(){
    X[] myArray = new X[2];
    myArray[0] = new A();
    myArray[1] = new B();
    
    for(int i=0;i<myArray.length;i++){
      myArray[i].intfcMethodX();
    }//end for loop

    System.out.println("");
  }//end doIt()

An array object of the interface type

In this case, rather than to declare the array object to be of type Object , the array is declared to be of the interface type X .

This is a less generic container than the one declared to be of type Object . Only references to objects instantiated from classes that implement the X interface, or objects instantiated from subclasses of those classes can be stored in the container. However, this is often adequate.

What methods can be called?

Since the references are stored as the interface type, any method declared in or inherited into the interface can be called on the references stored in the container. Of course, the objects referred to by those references must provide concrete definitions of those methods or the program won't compile.

(Although it isn't implicitly obvious, it is also possible to call any of the eleven methods defined in the Object class on an object's reference being stored as an interface type. Those eleven methods can be called on any object, including array objects, regardless of how the references are stored.)

Not the standard approach

If you are defining your own container, this is a satisfactory approach to implementation of the observer design pattern. However, you cannot use this approach when using containers from the standard collections framework, because those containers are designed to always store references as the generic type Object . In those cases, the casting solution of Question 7 (or the use of generics) is required.

Back to Question 8

Answer 7

C. A-intfcMethodX B-intfcMethodX

Explanation 7

The correct use of an interface

This program illustrates the correct use of an interface. It uses a cast of the interface type in the following fragment to resolve the problem that was discussed at length in Question 6 earlier.

Note:
  void doIt(){
    Object[] myArray = new Object[2];
    myArray[0] = new A();
    myArray[1] = new B();
    
    for(int i=0;i<myArray.length;i++){
      ((X)myArray[i]).intfcMethodX();
    }//end for loop

    System.out.println("");
  }//end doIt()

Back to Question 7

Answer 6

A. Compiler Error

Explanation 6

What is a container?

The word container is often used in Java, with at least two different meaning. One meaning is to refer to the type of an object that is instantiated from a subclass of the class named Container . In that case, the object can be considered to be of type Container , and typically appears in a graphical user interface (GUI) . That is not the usage of the word in the explanation of this program.

A more generic meaning

In this explanation, the word container has a more generic meaning. It is common to store a collection of object references in some sort of Java container, such as an array object or a Vector object. In fact, there is a complete collections framework provided to facilitate that sort of thing ( Vector is one of the concrete classes in the Java Collections Framework) .

Storing references as type Object

It is also common to declare the type of references stored in the container to be of the class Object . Because Object is a completely generic type, this means that a reference to any object instantiated from any class (or any array object) can be stored in the container. The standard containers such as Vector and Hashtable take this approach.

(Note that this topic became a little more complicated with the release of generics in jdk version 1.5.)

A class named Base and an interface named X

In a manner similar to several previous programs, this program defines a class named Base and an interface named X as shown in the following fragment.

Note:
class Base{
  public void inherMethod(){
    System.out.print("Base ");
  }//end inherMethod()
}//end class Base

interface X{
  public void intfcMethodX();
}//end X

Classes A and B extend Base and implement X

Also similar to previous programs, this program defines two classes named A and B . Each of these classes extends the class named Base and implements the interface named X , as shown in the next fragment.

Note:
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.)

Note:
  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 the above 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.

Note:
    for(int i=0;i<myArray.length;i++){
      myArray[i].intfcMethodX();
    }//end for loop

    System.out.println("");
  }//end doIt()

This produces the following compiler error under JDK 1.3:

Note:
Ap136.java:24: cannot resolve symbol
symbol  : method intfcMethodX  ()
location: class java.lang.Object
      
myArray[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 was instantiated will be identified and executed.

Otherwise, a cast is required

In order to call any method other than one of the eleven methods in the above list , (on an object's reference being stored as type Object without using generics) , you must cast the reference to some other type.

Casting to an interface type

The exact manner in which you write the cast will differ from one situation to the next. In this case, the problem can be resolved by rewriting the program using the interface cast shown in the following fragment.

Note:
  void doIt(){
    Object[] myArray = new Object[2];
    myArray[0] = new A();
    myArray[1] = new B();
    
    for(int i=0;i<myArray.length;i++){
      ((X)myArray[i]).intfcMethodX();
    }//end for loop

    System.out.println("");
  }//end doIt()

The observer design pattern

By implementing an interface, and using a cast such as this, you can store references to many different objects, of many different actual types, each of which implements the same interface, but which have no required superclass-subclass relationship, in the same container. Then, when needed, you can call the interface methods on any of the objects whose references are stored in the container.

This is a commonly used design pattern in Java, often referred to as the observer design pattern.

Registration of observers

With this design pattern, none, one, or more observer objects, (which implement a common observer interface) are registered on an observable object. This means references to the observer objects are stored in a container by the observable object.

Making a callback

When the observable object determines that some interesting event has occurred, the observable object calls a specific interface method on each of the observer objects whose references are stored in the container.

The observer objects execute whatever behavior they were designed to execute as a result of having been notified of the event.

The model-view-control (MVC) paradigm

In fact, there is a class named Observable and an interface named Observer in the standard Java library. The purpose of these class and interface definitions is to make it easy to implement the observer design pattern.

(The Observer interface and the Observable class are often used to implement a programming style commonly referred to as the MVC paradigm.)

Delegation event model, bound properties of Beans, etc.

Java also provides other tools for implementing the observer design pattern under more specific circumstances, such as the Delegation Event Model, and in conjunction with bound and constrained properties in JavaBeans Components.

Back to Question 6

Answer 5

C. A-intfcMethodX B-intfcMethodX

Explanation 5

More substantive use of an interface

This program illustrates a more substantive use of the interface than was the case in the previous programs.

The class named Base

The program defines a class named Base as shown in the following fragment.

Note:
class Base{
  public void inherMethod(){
    System.out.print("Base ");
  }//end inherMethod()
}//end class Base

The interface named X

The program also defines an interface named X as shown in the next fragment. Note that this interface declares a method named intfcMethodX .

Note:
interface X{
  public void intfcMethodX();
}//end X

Class A extends Base and implements X

The next fragment shows the definition of a class named A that extends Base and implements X .

Note:
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

Defining interface method

Because the class named A implements the interface named X , it must provide a concrete definition of all the methods declared in X .

In this case, there is only one such method. That method is named intfcMethodX . A concrete definition for the method is provided in the class named A .

Class B also extends Base and implements X

The next fragment shows the definition of another class (named B ), which also extends Base and implements X .

Note:
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

Defining the interface method

Because this class also implements X , it must also provide a concrete definition of the method named intfcMethodX .

Different behavior for interface method

However (and this is extremely important) , there is no requirement for this definition of the method to match the definition in the class named A , or to match the definition in any other class that implements X .

Only the method signature for the method named intfcMethodX is necessarily common among all the classes that implement the interface.

The definition of the method named intfcMethodX in the class named A is different from the definition of the method having the same name in the class named B .

The interesting behavior

The interesting behavior of this program is illustrated by the code in the following fragment.

Note:
  void doIt(){
    X myVar1 = new A();
    myVar1.intfcMethodX();
    X myVar2 = new B();
    myVar2.intfcMethodX();

    System.out.println("");
  }//end doIt()

Store object's references as interface type X

The code in the above fragment causes one object to be instantiated from the class named A , and another object to be instantiated from the class named B .

The two object's references are stored in two different reference variables, each declared to be of the type of the interface X .

Call the interface method on each reference

A method named intfcMethodX is called on each of the reference variables. Despite the fact that both object's references are stored as type X , the system selects and calls the appropriate method, (as defined by the class from which each object was instantiated) , on each of the objects. This causes the following text to appear on the screen:

Note:
A-intfcMethodX B-intfcMethodX

No subclass-superclass relationship exists

Thus, the use of an interface makes it possible to call methods having the same signatures on objects instantiated from different classes, without any requirement for a subclass-superclass relationship to exist among the classes involved.

In this case, the only subclass-superclass relationship between the classes named A and B was that they were both subclasses of the same superclass. Even that relationship was established for convenience, and was not a requirement.

Different behavior of interface methods

The methods having the same signature, (declared in the common interface, and defined in the classes) , need not have any similarity in terms of behavior.

A new interface relationship

The fact that both classes implemented the interface named X created a new relationship among the classes, which is not based on class inheritance.

Back to Question 5

Answer 4

C. Base A-intfcMethod

Explanation 4

Illustrates the use of an interface as a type

The program defines a class named Base , and a class named A , which extends Base , and implements an interface named X , as shown below.

Note:
class Base{
  public void inherMethod(){
    System.out.print("Base ");
  }//end inherMethod()
}//end class Base

class A extends Base implements X{
  public void inherMethod(){
    System.out.print(
                    " A-inherMethod ");
  }//end inherMethod()
    
  public void intfcMethod(){
    System.out.print("A-intfcMethod ");
  }//end intfcMethod()
}//end class A

interface X{
  public void intfcMethod();
}//end X

Implementing interfaces

A class may implement none, one, or more interfaces.

The cardinal rule on interfaces

If a class implements one or more interfaces, that class must either be declared abstract, or it must provide concrete definitions of all methods declared in and inherited into all of the interfaces that it implements. If the class is declared abstract, its subclasses must provide concrete definitions of the interface methods.

A concrete definition of an interface method

The interface named X in this program declares a method named intfcMethod . The class named A provides a concrete definition of that method.

(The minimum requirement for a concrete definition is a method that matches the method signature and has an empty body.)

Storing object's reference as an interface type

The interesting part of the program is shown in the following code fragment.

Note:
  void doIt(){
    Base myVar1 = new Base();
    myVar1.inherMethod();
    X myVar2 = new A();
    myVar2.intfcMethod();

    System.out.println("");
  }//end doIt()

The above fragment instantiates a new object of the class named A , and saves a reference to that object in a reference variable of the declared type X .

How many ways can you save an object's reference?

Recall that a reference to an object can be held by a reference variable whose type matches any of the following:

  • The class from which the object was instantiated.
  • Any superclass of the class from which the object was instantiated.
  • Any interface implemented by the class from which the object was instantiated.
  • Any interface implemented by any superclass of the class from which the object was instantiated.
  • Any superinterface of the interfaces mentioned above.

Save object's reference as implemented interface type

In this program, the type of the reference variable matches the interface named X , which is implemented by the class named A .

What does this allow you to do?

When a reference to an object is held by a reference variable whose type matches an interface implemented by the class from which the object was instantiated, that reference can be used to call any method declared in or inherited into that interface.

(That reference cannot be used to call methods not declared in or not inherited into that interface.)

In this simple case ...

The method named intfcMethod is declared in the interface named X and implemented in the class named A .

Therefore, the method named intfcMethod can be called on an object instantiated from the class named A when the reference to the object is held in a reference variable of the interface type.

(The method could also be called if the reference is being held in a reference variable of declared type A .)

The call to the method named intfcMethod causes the text A-intfcMethod to appear on the screen.

Back to Question 4

Answer 3

C. Base A-intfcMethod

Explanation 3

What is runtime polymorphic behavior?

One way to describe runtime polymorphic behavior is:

The runtime system selects among two or more methods having the same signature, not on the basis of the type of the reference variable in which an object's reference is stored, but rather on the basis of the class from which the object was originally instantiated.

Illustrates simple class and interface inheritance

The program defines a class named Base , and a class named A , which extends Base , and implements the interface named X , as shown in the following fragment.

Note:
class Base{
  public void inherMethod(){
    System.out.print("Base ");
  }//end inherMethod()
}//end class Base

class A extends Base implements X{
  public void inherMethod(){
    System.out.print(
                    " A-inherMethod ");
  }//end inherMethod()
    
  public void intfcMethod(){
    System.out.print("A-intfcMethod ");
  }//end intfcMethod()
}//end class A

interface X{
  public void intfcMethod();
}//end X

Define an interface method

The interface named X declares a method named intfcMethod . A concrete definition of that method is defined in the class named A .

A new object of type Base

The code in the following fragment instantiates a new object of the class Base and calls its inherMethod . This causes the word Base to appear on the output screen. There is nothing special about this. This is a simple example of the use of an object's reference to call one of its instance methods.

Note:
  void doIt(){
    Base myVar1 = new Base();
    myVar1.inherMethod();

A new object of type A

The following fragment instantiates a new object of the class A and calls its intfcMethod . This causes the text A-intfcMethod to appear on the output screen. There is also nothing special about this. This is also a simple example of the use of an object's reference to call one of its instance methods.

Note:
    A myVar2 = new A();
    myVar2.intfcMethod();

    System.out.println("");
  }//end doIt()

Not polymorphic behavior

The fact that the class named A implements the interface named X does not indicate polymorphic behavior in this case. Rather, this program is an example of simple class and interface inheritance.

Interface type is not used

The program makes no use of the interface as a type, and exhibits no polymorphic behavior (no decision among methods having the same signature is required) .

The class named A inherits an abstract method named intfcMethod from the interface and must define it. (Otherwise, it would be necessary to declare the class named A abstract.)

The interface is not a particularly important player in this program.

Back to Question 3

Answer 2

A. Compiler Error

Explanation 2

Simple hierarchical polymorphic behavior

This program is designed to test your knowledge of simple hierarchical polymorphic behavior.

Implement the interface named X

This program defines a class named A that extends a class named Base , and implements an interface named X , as shown in the following code fragment.

Note:
class A extends Base implements X{
  public void inherMethod(){
    System.out.print(
                    " A-inherMethod ");
  }//end inherMethod()
    
  public void intfcMethod(){
    System.out.print("A-intfcMethod ");
  }//end intfcMethod()
}//end class A

interface X{
  public void intfcMethod();
}//end X

Override and define some methods

The class named A overrides the method named inherMethod , which it inherits from the class named Base . It also provides a concrete definition of the method named intfcMethod , which is declared in the interface named X .

Store object's reference as superclass type

The program instantiates an object of the class named A and assigns that object's reference to a reference variable of type Base , as shown in the following code fragment.

Note:
    Base myVar2 = new A();

Oops! Cannot call this method

So far, so good. However, the next fragment shows where the program turns sour. It attempts to call the method named intfcMethod on the object's reference, which was stored as type Base .

Note:
    myVar2.intfcMethod();

Polymorphic behavior doesn't apply here

Because the class named Base does not define the method named intfcMethod , hierarchical polymorphic behavior does not apply. Therefore a reference to the object being stored as type Base cannot be used to directly call the method named intfcMethod , and the program produces a compiler error.

What is the solution?

Hierarchical polymorphic behavior is possible only when the class defining the type of the reference (or some superclass of that class) contains a definition for the method that is called on the reference.

There are a couple of ways that downcasting could be used to solve the problem in this case.

Back to Question 2

Answer 1

A. Compiler Error

Explanation 1

I put this question in here just to see if you are still awake.

Can store reference as interface type

A reference to an object instantiated from a class can be assigned to any reference variable whose declared type is the name of an interface implemented by the class from which the object was instantiated, or implemented by any superclass of that class.

Define two classes and an interface

This program defines a class named A that extends a class named Base . The class named Base extends Object by default.

The program also defines an interface named X .

Instantiate an object

The following statement instantiates an object of the class named A , and attempts to assign that object's reference to a reference variable whose type is the interface type named X .

Note:
    X myVar2 = new A();

Interface X is defined but not implemented

None of the classes named A , Base , and Object implement the interface named X . Therefore, it is not allowable to assign a reference to an object of the class named A to a reference variable whose declared type is X . Therefore, the program produces the following compiler error under JDK 1.3:

Note:
Ap131.java:20: incompatible types
found   : A
required: X
    X myVar2 = new A();

Back to Question 1

-end-

Content actions

Download module as:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks