G
G
godlikenich2017-01-18 19:10:09
Java
godlikenich, 2017-01-18 19:10:09

Why can't you just call clone?

Good. I’m learning Java according to Horstmann and the following question appeared. If the Object class has a clone () method protected, then I know that all sub-classes can call it. But in our case, all classes implicitly inherit the Object class, so why can’t I call?
PS: Yes, I know that it is necessary to write implements Cloneable, and rewrite the clone method, making it public.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pazukdev, 2021-01-20
@pazukdev

Why not when you can?

public class SomeSubClass {
    
    void someMethod() throws CloneNotSupportedException {
        this.clone();
    }
    
}

This code compiles.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question