I know that the byteValue() and shortValue() have implementations unlike the other abstract methods and were added in JDK1.1. This wouldn't have been possible if it was an Interface. But when the developers were writing Number class why did they make it an abstract class? Was it only because they expected that they might add more methods later on? I would only need answers supported by authoritative citations. thanks a lot everyone for the time you are taking to review my question and give answers.
Nobody here will know what went on in the minds of the designers, however abstract classes and interfaces are used for different purposes.
Classes (in Java) inherit in a strict hierarchy, and this hierarchy is a tool that can be used to ensure seperation of unrelated classes of objects. Classes are also more logical when the core functionality of the entire hierarchy is similar.
For example, with abstract classes Number
and Letter
it would not be possible to have a class that is both. With interfaces one could create a class that implements both which would make no sense.
Interfaces on the other hand are often used to expose a (usually) small piece of a class in a formal way so they can be used by reusable logic that uses only the functionality specified in the interface. They're more often used for adding supporting functionality, like Serializable
, Comparable
or Runnable
.
An example, Printable
and Comparable
would be terrible abstract classes, as it would make it impossible to have a Comparable
object also be Printable
.
So the designers may have specifically chosen to make Number
an abstract class to ensure that only one hierarchy of classes can be numbers, and nothing else. Perhaps it may allow for future optimizations where the JDK treats these classes as special cases, just like it does for String
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With