Java Reference
In-Depth Information
15.2
The getArea and getPerimeter methods may be removed from the
GeometricObject class. What are the benefits of defining getArea and
getPerimeter as abstract methods in the GeometricObject class?
15.3
True or false?
a. An abstract class can be used just like a nonabstract class except that you cannot
use the new operator to create an instance from the abstract class.
b. An abstract class can be extended.
c. A subclass of a nonabstract superclass cannot be abstract.
d. A subclass cannot override a concrete method in a superclass to define it as abstract.
e. An abstract method must be nonstatic.
15.3 Case Study: the Abstract Number Class
Number is an abstract superclass for numeric wrapper classes, BigInteger , and
BigDecimal .
Section 10.12 introduced numeric wrapper classes and Section 10.14 introduced the
BigInteger and BigDecimal classes. These classes have common methods
byteValue() , shortValue() , intValue() , longValue() , floatValue() , and
doubleValue() for returning a byte , short , int , long , float , and double value from
an object of these classes. These common methods are actually defined in the Number class,
which is a superclass for the numeric wrapper classes, BigInteger , and BigDecimal , as
shown in Figure 15.2.
Key
Point
java.lang.Number
+byteValue(): byte
+shortValue(): short
+intValue(): int
+longVlaue(): long
+floatValue(): float
+doubleValue():double
Double
Float
Long
Integer
Short
Byte
BigInteger
BigDecimal
F IGURE 15.2 The Number class is an abstract superclass for Double , Float , Long , Integer , Short , Byte ,
BigInteger and BigDecimal .
Since the intValue() , longValue() , floatValue() , and doubleValue() methods
cannot be implemented in the Number class, they are defined as abstract methods in the
Number class. The Number class is therefore an abstract class. The byteValue() and
shortValue() method are implemented from the intValue() method as follows:
public byte byteValue() {
return ( byte )intValue();
}
 
 
 
Search WWH ::




Custom Search