You are on page 1of 4

Chapter 14 Abstract Classes and Interfaces

1. E and F

2. The benefits are for generic programming. A variable of GeometricObject type can use the getArea and get erimeter methods at compilation time. !. ". #. True d. True

$. %sing &omparable as the parameter type is simpler and more robust than using Object. '. The benefits are for generic programming. A variable of the class that implements &omparable can be passed to a method that re(uires a &omparable type. ). True *. +o. ,es- the .ate class implements &loneable. 1/. 0f the &loneable&ircle class does not override the clone12 method- the program 3ould receive a synta4 error because clone12 is protected in java.lang.Object. 0f &loneable&ircle does not implement java.lang.&loneable in 5ine !1- the program 3ould receive a runtime error because c2 3ould be null in 5ine '. 11. true false true 12.
true false list is 6+e3 ,or7- Atlanta8 list1 is 6+e3 ,or7- Atlanta8 list2.get1/2 is +e3 ,or7 list2.si9e12 is 1

1!. A synta4 error is reported because clone12 is protected in Object. To enable cloning- do t3o things: 112

override clone12 in the class for the object to be cloned; 122 implement java.lang.&loneable for the class. 1". 1#. <ee the e4ample in the te4t in the section on 0nterfaces vs. Abstract &lasses. <ee the section = rocessing rimitive Type >alues as Objects.= These classes are useful 3hen passing numerical values as objects. 0nteger i ? ne3 0nteger1=2!=2; Ans3er: &orrect 0nteger i ? ne3 0nteger12!2; Ans3er: &orrect 0nteger i ? 0nteger.valueOf1=2!=2; Ans3er: &orrect 0nteger i ? 0nteger.parse0nt1=2!=-)2; Ans3er: 0ncorrect .ouble d ? ne3 .ouble12; Ans3er: 0ncorrect- no default constructor in .ouble .ouble d ? .ouble.valueOf1=2!."#=2; Ans3er: &orrect int i ? 10nteger.valueOf1=2!=22.int>alue12; Ans3er: &orrect double d ? 1.ouble.valueOf1=2!."=22.double>alue12; Ans3er: &orrect int i ? 1.ouble.valueOf1=2!."=22.int>alue12; Ans3er: &orrect <tring s ? 1.ouble.valueOf1=2!."=22.to<tring12; Ans3er: &orrect 1'. %se ne3 0nteger1int2.to<tring12 to convert an integer to a string. %se ne3 .ouble1double2.to<tring12 to convert a double to a string.

1$.

1).

At runtime- @>A attempts to convert numberBef to a .ouble object- but numberBef is an instance of 0ntegernot .ouble. numberArray6/8is of the 0nteger type. The program has a synta4 error because 4 does not have the compareTo method. The program has a synta4 error because the member access operator 1.2 is e4ecuted before the casting operator.

1*. 2/. 21.

22.
1/ 1/ 1$ 11 11 1'

2!.
+umber 4 ? !; CC &orrect- this is same as 4 ? ne3 0nteger1!2 0nteger 4 ? !; CC &orrect .ouble 4 ? !; CC Drong- this is same as .ouble 4 ? ne3 0nteger1!2 .ouble 4 ? !./; CC &orrect int 4 ? ne3 0nteger1!2; CC &orrect int 4 ? ne3 0nteger1!2 E ne3 0nteger1"2; CC &orrect double y ? !."; y.int>alue12; CC Drong @Option ane.sho3Aessage.ialog1null- "#.#2; CC &orrect. ,ou can pass a primitive type value 3herever an object is re(uired.

2". ne3 int61/8 cannot be assigned to into a variable of Object68 type- but ne3 <tring61//8- ne3 Object6#/8- or ne3 &alendar62/8 are fine. 2#. The output is !. lease note that Fig0nteger and Fig.ecimal are immutable. Find these terms in this chapter.

2$. 2'.

0ndicate true or false for the follo3ing statements: 1. An abstract class can have instances created using the constructor of the abstract class. Answer: +o- but an instance of its concrete subclass is also an instance of the parent abstract class. ,ou

cannot create an object using the constructor of the abstract class. Go3everyou can create an object using a concrete subclass of the abstract class. This object is an instance of the subclass and it is also an instance of the abstract class.

2. !.

An abstract class can be e4tended. Answer: True An interface can be a separate unit and can be compiled into a bytecode file. Answer: True

" A subclass of a nonHabstract superclass cannot be abstract. Answer: False # A subclass cannot override a concrete method in a superclass to declare it abstract. Answer: False- This is rare- but useful 3hen the implementation of the method in the superclass becomes invalid in the subclass. 0n this case- the subclass must be declared abstract. $. An abstract method must be nonHstatic Answer: True ' An interface can have static methods. Answer: False ) An interface can e4tend one or more interfaces. Answer: True *. An interface can e4tend an abstract class. Answer: False 1/. An abstract class can e4tend an interface. Answer: False

You might also like