Java Reference
In-Depth Information
13.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?
13.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.
Number
is an abstract superclass for numeric wrapper classes,
BigInteger
, and
BigDecimal
.
Key
Point
Section 10.7 introduced numeric wrapper classes and Section 10.9 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 13.2.
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
13.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 can-
not 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();
}
public short
shortValue() {
return
(
short
)intValue();
}
Search WWH ::
Custom Search