Will this create problems if Number should be implemented as an interface? Are there any advantages?

In a recent question, I came to use the abstract class Number .

Now that Java 8 is here, there are default methods, so Number can be an interface and written as:

 public interface Number { public int intValue(); public long longValue(); public float floatValue(); public double doubleValue(); default public byte byteValue() { return (byte)intValue(); } default public short shortValue() { return (short)intValue(); } } 

Will old code use the abstract class Number , if so? Are there any real benefits to creating a Number interface, rather than an abstract class?

+6
source share
1 answer

Such changes will, of course, immediately violate the API and cause chaos in the Java programming world. You simply cannot change the class (this is also the one that existed since the advent of Java) into the interface, because both have different meanings. And both are used in different ways.

For example, as soon as you create the Number interface, the immediate subclasses of, say, Integer will be broken because the class cannot extend from the interface, but must implement it. But the Integer class continues from Number .

You can argue that by changing the Number Java API designer can also appropriately change the necessary subclasses in the Java API. But they cannot ignore the fact that someone else may have their own classes that extend from Number .

One more thing regarding the fact from which this question arose - interface with standard methods does not coincide with the abstract class. This has been discussed quite a few times on SO itself , one of which is answered only by you.

+1
source

All Articles