Why is the Number class in Java not an abstract, but an interface?

I know that byteValue () and shortValue () have implementations unlike other abstract methods and were added in JDK1.1. This would not be possible if it were an interface. But when the developers wrote the Number class, why did they make it an abstract class? Was it just because they expected that later they could add more methods? I only need answers, confirmed by authoritative quotes . Many thanks to everyone for accepting me to review my question and provide answers.

+4
source share
1 answer

No one here will know what is happening in the minds of designers, but abstract classes and interfaces are used for different purposes.

Classes (in Java) inherit a strict hierarchy, and this hierarchy is a tool that can be used to ensure separation of unrelated classes of objects. Classes are also more logical when the basic functionality of the entire hierarchy is similar.

For example, with abstract classes Numberand Letterit would be impossible to have a class that is simultaneously. With the interfaces, you could create a class that implements both functions that make no sense.

, , , () , , , . , Serializable, Comparable Runnable.

: Printable Comparable , , Comparable Printable.

, , Number , , , . , , JDK , String.

+1

Source: https://habr.com/ru/post/1672737/


All Articles