Inheritance Across Packages

强颜欢笑 提交于 2021-01-29 05:51:08

问题


Let's say we have package a and package b, both have many classes, all public.

Now class Cat from package a wants to extend class Animal from package b, is this inheritance legal?


回答1:


ASure, just import the parent class and extend it.

package a;

import b.Animal;

public class Cat extends Animal {

}

You can skip the import and use the fully qualified class name for Animal as well. But messier.




回答2:


Yes. Packages make grouping classes convenient, and non-public and non-private methods and fields are only visible to other members of the same package



来源:https://stackoverflow.com/questions/16312010/inheritance-across-packages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!