Reputation: 7427
class Main {
public static void main(String[] args) {
new Cloned().clone();
}
}
class Cloned implements Cloneable {
}
This gives an error, saying it is protected. None of the subclasses of object can call that method.
Upvotes: 5
Views: 662
Reputation: 4114
class Main implements Cloneable {
public static void main(String[] args) {
}
@Override
protected Object clone() throws CloneNotSupportedException {
//TODO Auto-generated method stub
return super.clone();
}
}
Upvotes: 1
Reputation: 421340
The problem here is that Main
doesn't extend Cloned
. As it stands, Main
can call Main.clone
, but not Cloned.clone
.
The fact that the clone
method is declared in Object
doesn't matter here. Apart from public methods, a class can only call its own inherited methods. This includes the protected ones from its super-classes, but not protected methods of others (super) classes.
Upvotes: 4
Reputation: 7024
because clone()
is protected method in the class Object
if you want clone()
to be accessed publically, u need to override that method in ur class.
@override
public Object clone()
{
return super.clone();
}
Upvotes: 7