简体   繁体   中英

Java: why we need to extend a class when we can access the fields and methods from non sub class

I am a bit confused about Inheritance. We extend a class so that its fields and methods are inherited by the sub class with the ultimate goal of being able to use those fields and methods in the sub class. But for a public class, we can access the fields and methods from a non sub class from wihin and outside the package also. Then what is the need to extend a class? We can also access private fields with the help of public getters and setters from non sub class (both from within and outside package).

通过扩展类,所有方法和变量都将被继承,并且您可以拥有自己的副本,从而可以分配diff值和参数

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM