Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java interface extends Cloneable

Tags:

java

cloneable

I don't understand why we can't do the following:

interface MyInterface extends Cloneable {}

class myClazz implements MyInterface {
    public Object clone() { return null; }
}

class test{
    public static void main(String[]a){
        MyInterface o = new myClazz();
        o.clone(); // IMPOSSIBLE
    }
}

But this will work fine

interface Misc{
    public void testM();
}

interface MyInterface extends Misc{}

class myClazz implements MyInterface {
    public void testM() {}
}

class test{
    public static void main(String[]a){
        MyInterface o = new myClazz();
        o.testM(); // OK
    }
}

What's happen with Cloneable?

Thanks,

like image 290
JohnJohnGa Avatar asked Sep 26 '26 18:09

JohnJohnGa


1 Answers

The Cloneable interfaces doesn't have any methods.
It's just a marker interface which the base Object.clone method (which is protected) checks for.

If you want a clone method, you need to declare it yourself.

like image 57
SLaks Avatar answered Sep 30 '26 14:09

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!