0
public class TestClone extends Person {

    public  static void main(String[] args) {
        Person p = new Person();
        p.clone();
    }

}

//there's a compile error here (**clone() has protected access in 'java.lang.Object'). I know how to use clone() in java. The point here is, class Person extends Object, then Person should be able to use clone() in Object because Protected method in superclass is visible to subclass. But here comes a compile error, why...

BamsBamx
  • 4,139
  • 4
  • 38
  • 63
LeonLiu
  • 1
  • 2
  • 5
    It's protected, so you can call it from *within* an instance of TestClone. You are calling it from a main method which is static and so not within an instance of TestClone. – Michael Jan 16 '18 at 16:15
  • `Person'`s instance methods can use `clone()` from the inside: `Object foo() { return this.clone(); }` should compile perfectly fine. `p.clone()` inside `main` is a "call from the outside", though, so it is disallowed. – Sergey Kalinichenko Jan 16 '18 at 16:20

0 Answers0