<< Chapter < Page | Chapter >> Page > |
Revised: Wed May 11 18:13:42 CDT 2016
This page is included in the following Books:
This module is one in a collection of modules on Java Generics designed for teaching ITSE2317 - Java Programming (Intermediate) at Austin Community College in Austin, TX.
This and future modules in this series will be based on the Generics (Updated) section of The Java Tutorials from Oracle. (In the event that these links become broken, you should have no difficulty finding the Oracle material with an online search.)
You will find the learning resources for this module on the Oracle site at Generics, Inheritance, and Subtypes .
This module contains review questions and answers keyed to the material in that document.
Once you study that material, you should be able to answer the review questions in this module.
Once you complete your study of all the material on generics , you should be able to answer the review questions at Questions and Exercises on the Oracle site.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to navigate from the question to the answer and back again.
True or False: The code shown in Listing 1 will compile and run successfully producing the output shown in Figure 1 .
Listing 1 - Question 1. |
---|
/*File Q01.java
************************************************//*
Integer and Double are both subclasses ofNumber. Both classes define a method named
doubleValue that returns the encapsulatednumeric value as type double.
*/public class Q01{
public static void main(String[]args){
Foo<Integer>iFoo = new Foo<Integer>(15);
System.out.println(iFoo.get().doubleValue());Foo<Double>dFoo = new Foo<Double>(1.0/3);
System.out.println(dFoo.get().doubleValue());}//end main
}//end class Q01//=============================================//
class Foo<T extends Number>{
private T obj;public Foo(T obj){
this.obj = obj;}//end constructor
public T get(){return obj;
}//end get}//end Foo
//=============================================// |
Figure 1 - Question 1. |
---|
15.0
0.3333333333333333 |
True or False: The code shown in Listing 2 will compile and run successfully producing the output shown in Figure 2 .
Listing 2 - Question 2. |
---|
/*File Q02.java
************************************************/import java.awt.Frame;
import javax.swing.JInternalFrame;import java.awt.Container;
/*Frame and JInternalFrame are both subclasses of
Container. Both classes define a constructorthat accepts a String as a title. Both classes
define a method named getTitle that returns thetitle string.
*/public class Q02{
public static void main(String[]args){
Foo<Frame>fFoo =
new Foo<Frame>(
new Frame("Frame title"));System.out.println(fFoo.get().getTitle());
Foo<JInternalFrame>jFoo =
new Foo<JInternalFrame>(
new JInternalFrame("JInternalFrame title"));
System.out.println(jFoo.get().getTitle());}//end main
}//end class Q02//=============================================//
class Foo<T extends Container>{
private T obj;public Foo(T obj){
this.obj = obj;}//end constructor
public T get(){return obj;
}//end get}//end Foo
//=============================================// |
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?