How are java.lang.Object's protected methods protected from subclasses?

后端 未结 3 417
难免孤独
难免孤独 2020-12-28 10:18

The keyword protected grants access to classes in the same package and subclasses (http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html).

3条回答
  •  长发绾君心
    2020-12-28 10:38

    The difference is how you are accessing Object.clone(). clone is only accessible when accessed via a sub class or class in the same package. In the getOne() example you are claling this.clone(). This clearly satisfies access from within a sub class.

    In getTwo() though you are acessing Object.clone() and not TestClass.clone(). In order for this to work you must have package level access to Object which you don't and hence the error.

提交回复
热议问题