0

The following code compiles w/o errors:

public interface ICloneable {

    default ICloneable clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    public static void main(String[] args) {

        var object = new ICloneable() {

        };


        System.out.println("ok");
    }
}

But at runtime it says:

ICloneable.java:9: error: clone() in Object cannot implement clone() in ICloneable
        var object = new ICloneable() {
                                      ^
  attempting to assign weaker access privileges; was public

Is it possible to overcome?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • you need to Override the clone method on the new ICloneable() {}; call – Anon Dec 14 '22 at 23:02
  • The idea of `default` is not to override :) – Dims Dec 14 '22 at 23:04
  • if you want to use the default then create a class that implements that interface then instaciate that class other then that you need to Override the method – Anon Dec 14 '22 at 23:06
  • Possibly related: [Java8: Why is it forbidden to define a default method for a method from java.lang.Object](https://stackoverflow.com/q/24016962) – Pshemo Dec 14 '22 at 23:30

1 Answers1

3

The following code compiles w/o errors

No it doesn't.

That error you see is in fact a compile error.

Why you get that error

I'm not sure if you understand that clone() is, itself, a method that java.lang.Object already has: javadoc of Object.clone().

A default implementation in an interface never overrides any implementation found anywhere in a class's actual class hierarchy. Meaning, your clone() default impl in your IClonable interface is never used - as all classes have Object in their hierarchy and its clone() takes precedence.

However, IClonable must have a public clone() method - but the clone() you get 'for free' from j.l.Object is protected, hence your code does not compile here.

Is it possible to overcome?

Sure. Various options exist.

The best: Don't call it clone()

Given that clone() is already taken by j.l.Object, find another word. Unless, of course, your intent was to use an interface to force both a default impl and force it public, in which case...

Use abstract classes instead

If ICloneable was an abstract class, you wouldn't get this error.

... otherwise

No, this cannot be fixed. There is no way to tell javac to take the default impl of an interface even though an impl exists in the class hierarchy.

NB: Using IClonable style naming is discouraged. Using the clone() functionality sort of half baked into java (and Object's own clone() is part of this functionality) is strongly discouraged. Make immutable types where possible. If not possible, think through what cloning might mean and just make a method for it. The general principle of 'you can clone me' is broken as a concept. For example, cloning an immutable is pointless, yet, having the clone() method of an immutable type be written as public Object clone() { return this; } is misleading. As a concept, it's not salvagable. There's a reason e.g. arraylist has new ArrayList<T>(otherArraylist) as suggested way to clone one, not List<T> clone = otherArrayList.clone();.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Then, can I clone with some othe class, like I can fix flaws of `Object`-s `toString` with `Objects`'? For example, it could exist `Objects#clone` method? – Dims Dec 15 '22 at 21:21
  • "Just clone this" as a concept is broken as I explained. In java, it's not even clear what 'please clone this arbitrary object' even means! – rzwitserloot Dec 15 '22 at 21:28
  • To deal with "broken concepts" a polymorphism was invented: authors of some library are able to coin their own specific concept, valid within their library – Dims Dec 17 '22 at 09:58
  • Sure. But you need to define what 'clone' means, your objects cannot contain arbitrary objects (only objects of types within your own little space that has very well defined cloning behaviour), and thus `Objects.clone` should not exist (it should perhaps be called `Shapes.clone` if you wrote a shapes library, etc. – rzwitserloot Dec 17 '22 at 20:14