Skip to content Skip to navigation

OpenStax-CNX

You are here: Home » Content » Ap0110: Self-assessment, Extending classes, overriding methods, and polymorphic behavior

Navigation

Recently Viewed

This feature requires Javascript to be enabled.
 

Ap0110: Self-assessment, Extending classes, overriding methods, 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 extending classes, overriding methods, 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. A
  • D. None of the above.
1
Listing 1: Listing for Question 1.
public class Ap120{
  public static void main(
                        String args[]){
    new Worker().doIt();
  }//end main()
}//end class Ap120

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

class Base{
}//end class Base

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

Answer and Explanation

Question 2

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

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

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

class Base{
}//end class Base

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

Answer and Explanation

Question 3

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

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

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

class Base{
  abstract public void test();
}//end class Base

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

Answer and Explanation

Question 4

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

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

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

abstract class Base{
  abstract public void test();
}//end class Base

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

Answer and Explanation

Question 5

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

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

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

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

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

Answer and Explanation

Question 6

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

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

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

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

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

Answer and Explanation

Question 7

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

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

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

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

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

Answer and Explanation

Question 8

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

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

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

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

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

Answer and Explanation

Question 9

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

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

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

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

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

Answer and Explanation

Question 10

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

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

class Worker{
  void doIt(){
    Base 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 class Base

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

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

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: Ap0110: Self-assessment, Extending classes, overriding methods, and polymorphic behavior
  • File: Ap0110.htm
  • Originally published: 2002
  • Published at cnx.org: 12/08/12
  • Revised: 03/12/14

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

D. A B

Explanation 10

Another illustration of simple polymorphic behavior

In this program, two classes named A and B extend the class named Base , each overriding the method named test to produce different behavior. (Typically, overridden methods in different classes will produce different behavior, even though they have the same names.)

Behavior appropriate for object on which method is called

In other words, the behavior of the method named test , when called on a reference to an object of type A , is different from the behavior of the method named test when called on a reference to an object of type B .

The method definitions

The definitions of the two classes named A and B , along with the two versions of the overridden method named test are shown in the following fragment.

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

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

Store a subclass object's reference as a superclass type

The program declares a reference variable of type Base , instantiates a new object of the class named A , and assigns that object's reference to the reference variable of type Base . Then it calls the method named test on that reference as shown in the following fragment.

Note:
    Base myVar = new A();
    myVar.test();

Polymorphic behavior applies

Simple polymorphic behavior causes the overridden version of the method named test , defined in the class named A , (as opposed to the versions defined in class Base or class B ) to be executed. This causes the letter A followed by a space character to be displayed on the standard output device.

Store another subclass object's reference as superclass type

Then the program instantiates a new object from the class named B , and assigns that object's reference to the same reference variable, overwriting the reference previously stored there. (This causes the object whose reference was previously stored in the reference variable to become eligible for garbage collection in this case.)

Then the program calls the method named test on the reference as shown in the following fragment.

Note:
    myVar = new B();
    myVar.test();

Polymorphic behavior applies again

This time, simple polymorphic behavior causes the overridden version of the method named test , defined in the class named B , (as opposed to the versions defined in class Base or class A ) to be executed. This causes the letter B followed by a space character to be displayed on the standard output device.

Once again, what is runtime polymorphic behavior?

With runtime polymorphic behavior, the method selected for execution is based, not on the type of the reference variable holding the reference to the object, but rather on the actual class from which the object was instantiated.

If the method was properly overridden, the behavior exhibited by the execution of the method is appropriate for an object of the class from which the object was instantiated.

Back to Question 10

Answer 9

D. A

Explanation 9

Compiles and executes successfully

This program compiles and executes successfully causing the version of the method named test , which is overridden in the class named A to be executed. That overridden method is shown in the following fragment.

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

So, what is the issue here?

The purpose of this program is to determine if you understand polymorphic behavior and the role of downcasting. Consider the following fragment taken from the program in Question 8 .

Note:
    Base myVar = new A();
    ((A)myVar).test();

The downcast is redundant

As you learned in the discussion of Question 8 , the downcast isn't required, and it has no impact on the behavior of the program in Question 8 .

This program behaves exactly the same with the second statement in the above fragment replaced by the following statement, which does not contain a downcast.

Note:
    myVar.test();

Again, you need to know when downcasting is required, when it isn't required, and to make use of that knowledge to downcast appropriately.

Back to Question 9

Answer 8

D. A

Explanation 8

Compiles and executes successfully

This program compiles and executes successfully causing the version of the method named test , which is overridden in the class named A to be executed. That overridden method is shown in the following fragment.

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

So, what is the issue here?

The purpose of this program is to determine if you understand polymorphic behavior and the role of downcasting, as shown in the following fragment.

Note:
    Base myVar = new A();
    ((A)myVar).test();

This would be a simple case of polymorphic behavior were it not for the downcast shown in the above fragment.

The downcast is redundant

Actually, the downcast was placed there to see if you could determine that it is redundant. It isn't required, and it has no impact on the behavior of this program. This program would behave exactly the same if the second statement in the above fragment were replaced with the following statement, which does not contain a downcast.

Note:
    myVar.test();

You need to know when downcasting is required, when it isn't required, and to make use of that knowledge to downcast appropriately.

Back to Question 8

Answer 7

B. Runtime Error

Explanation 7

Storing a reference as a superclass type

You can store an object's reference in any reference variable whose declared type is a superclass of the actual class from which the object was instantiated.

May need to downcast later

Later on, when you attempt to make use of that reference, you may need to downcast it. Whether or not you will need to downcast will depend on what you attempt to do.

In order to call a method ...

For example, if you attempt to call a method on the reference, but that method is not defined in or inherited into the class of the reference variable, then you will need to downcast the reference in order to call the method on that reference.

Class Base defines method named test

This program defines a class named Base that defines a method named test .

Class A extends Base and overrides test

The program also defines a class named A that extends Base and overrides the method named test as shown in the following fragment.

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

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

A new object of the class Base

The program instantiates a new object of the class Base and stores a reference to that object in a reference variable of type Base , as shown in the following fragment.

Note:
    Base myVar = new Base();
    ((A)myVar).test();

Could call test directly on the reference

Having done this, the program could call the method named test directly on the reference variable using a statement such as the following, which is not part of this program.

Note:
    myVar.test();

This statement would cause the version of the method named test defined in the class named Base to be called, causing the word Base to appear on the standard output device.

This downcast is not allowed

However, this program attempts to cause the version of the method named test defined in the class named A to be called, by downcasting the reference to type A before calling the method named test . This is shown in the following fragment.

Note:
    ((A)myVar).test();

A runtime error occurs

This program compiles successfully. However, the downcast shown above causes the following runtime error to occur under JDK 1.3:

Note:
Exception in thread "main" java.lang.ClassCastException: Base
        at Worker.doIt(Ap126.java:22)
        at Ap126.main(Ap126.java:15)

What you can do

You can store an object's reference in a reference variable whose type is a superclass of the class from which the object was originally instantiated. Later, you can downcast the reference back to the type (class) from which the object was instantiated.

What you cannot do

However, you cannot downcast an object's reference to a subclass of the class from which the object was originally instantiated.

Unfortunately, the compiler is unable to detect an error of this type. The error doesn't become apparent until the exception is thrown at runtime.

Back to Question 7

Answer 6

C. Base

Explanation 6

Totally straightforward code

This rather straightforward program instantiates an object of the class named Base and assigns that object's reference to a reference variable of the type Base as shown in the following fragment .

Note:
    Base myVar = new Base();
    myVar.test();

Then it calls the method named test on the reference variable.

Class Base defines the method named test

The class named Base contains a concrete definition of the method named test as shown in the following fragment. This is the method that is called by the code shown in the above fragment .

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

Class A is just a smokescreen

The fact that the class named A extends the class named Base , and overrides the method named test , as shown in the following fragment, is of absolutely no consequence in the behavior of this program. Hopefully you understand why this is so. If not, then you still have a great deal of studying to do on Java inheritance.

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

Back to Question 6

Answer 5

A. Compiler Error

Explanation 5

Cannot instantiate an abstract class

This program defines an abstract class named Base . Then it violates one of the rules regarding abstract classes, by attempting to instantiate an object of the abstract class as shown in the following code fragment.

Note:
    Base myVar = new Base();

The program produces the following compiler error under JDK 1.3:

Note:
Ap124.java:19: Base is abstract; cannot be instantiated
    Base myVar = new Base();

Back to Question 5

Answer 4

C. A

Explanation 4

An abstract class with an abstract method

This program illustrates the use of an abstract class containing an abstract method to achieve polymorphic behavior .

The following code fragment shows an abstract class named Base that contains an abstract method named test .

Note:
abstract class Base{
  abstract public void test();
}//end class Base

Extending abstract class and overriding abstract method

The class named A , shown in the following fragment extends the abstract class named Base and overrides the abstract method named test .

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

Can store a subclass reference as a superclass type

Because the class named A extends the class named Base , a reference to an object instantiated from the class named A can be stored in a reference variable of the declared type Base . No cast is required in this case.

Polymorphic behavior

Furthermore, because the class named Base contains the method named test , (as an abstract method) , when the method named test is called on a reference to an object of the class named A , stored in a reference variable of type Base , the overridden version of the method as defined in the class named A will actually be called. This is polymorphic behavior.

(Note, however, that this example does little to illustrate the power of polymorphic behavior because only one class extends the class named Base and only one version of the abstract method named test exists. Thus, the system is not required to select among two or more overridden versions of the method named test .)

The important code

The following code fragment shows the instantiation of an object of the class named A and the assignment of that object's reference to a reference variable of type Base . Then the fragment calls the method named test on the reference variable.

Note:
    Base myVar = new A();
    myVar.test();

This causes the overridden version of the method named test , shown in the following fragment, to be called, which causes the letter A to be displayed on the standard output device.

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

Back to Question 4

Answer 3

A. Compiler Error

Explanation 3

Classes can be final or abstract, but not both

A class in Java may be declared final . A class may also be declared abstract . A class cannot be declared both final and abstract .

Behavior of final and abstract classes

A class that is declared final cannot be extended. A class that is declared abstract cannot be instantiated. Therefore, it must be extended to be useful.

An abstract class is normally intended to be extended.

Methods can be final or abstract, but not both

A method in Java may be declared final . A method may also be declared abstract . However, a method cannot be declared both final and abstract .

Behavior of final and abstract methods

A method that is declared final cannot be overridden. A method that is declared abstract must be overridden to be useful.

An abstract method doesn't have a body.

Abstract classes and methods

A class that contains an abstract method must itself be declared abstract . However, an abstract class is not required to contain abstract methods.

Failed to declare the class abstract

In this program, the class named Base contains an abstract method named test , but the class is not declared abstract as required.

Note:
class Base{
  abstract public void test();
}//end class Base

Therefore, the program produces the following compiler error under JDK 1.3:

Note:
Ap122.java:24: Base should be declared abstract; 
it does not define test in Base
class Base{

Back to Question 3

Answer 2

C. A

Explanation 2

If you missed this ...

If you missed this question, you didn't pay attention to the explanation for Question 1 .

Define a method in a subclass

This program defines a subclass named A that extends a superclass named Base . A method named test is defined in the subclass named A but is not defined in any superclass of the class named A .

Store a reference as a superclass type

The program declares a reference variable of the superclass type, and stores a reference to an object of the subclass in that reference variable as shown in the following code fragment.

Note:
    Base myVar = new A();

Downcast and call the method

Then the program calls the method named test on the reference stored as the superclass type, as shown in the following fragment.

Note:
    ((A)myVar).test();

Unlike the program in Question 1 , the reference is downcast to the true type of the object before calling the method named test . As a result, this program does not produce a compiler error.

Why is the cast required?

As explained in Question 1 , it is allowable to store a reference to a subclass object in a variable of a superclass type. Also, as explained in Question 1 , it is not allowable to directly call, on that superclass reference, a method of the subclass object that is not defined in or inherited into the superclass.

However, such a call is allowable if the programmer purposely downcasts the reference to the true type of the object before calling the method.

Back to Question 2

Answer 1

A. Compiler Error

Explanation 1

Define a method in a subclass

This program defines a subclass named A that extends a superclass named Base . A method named test , is defined in the subclass named A , which is not defined in any superclass of the class named A .

Store a reference as superclass type

The program declares a reference variable of the superclass type, and stores a reference to an object of the subclass in that reference variable as shown in the following code fragment.

Note:
    Base myVar = new A();

Note that no cast is required to store a reference to a subclass object in a reference variable of a superclass type. The required type conversion happens automatically in this case.

Call a method on the reference

Then the program attempts to call the method named test on the reference stored as the superclass type, as shown in the following fragment. This produces a compiler error.

Note:
    myVar.test();

The reason for the error

It is allowable to store a reference to a subclass object in a variable of a superclass type. However, it is not allowable to directly call, (on that superclass reference) , a method of the subclass object that is not defined in or inherited into the superclass.

The following error message is produced by JDK 1.3.

Note:
Ap120.java:18: cannot resolve symbol
symbol  : method test  ()
location: class Base
    myVar.test();

The solution is ...

This error can be avoided by casting the reference to type A before calling the method as shown below:

Note:
    ((A)myVar).test();

Back to Question 1

-end-

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

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