类的继承是指在一个现有类的基础上去构建一个新的类,构建出来的新类被称作子类,现有类被称作父类,子类会自动拥有父类所有可继承的属性和方法。
继承的好处:
1、继承的出现提高了代码的复用性,提高软件开发效率。
2、继承的出现让类与类之间产生了关系,提供了多态的前提。
在类的继承中,需要注意一些问题,具体如下:
1、在Java中,类只支持单继承,不允许多继承,也就是说一个类只能有一个直接父类,
2、多个类可以继承一个父类
3、在Java中,多层继承是可以的
4、子类方法覆盖父类方法叫重写,必须要保证权限大于等于父类权限。
写法上稍微注意:必须一模一样:方法的返回值类型 方法名 参数列表都要一样
5、父类的构造无法继承。
继承成员变量的特点(就近原则)
子父类的成员变量的名字不一样直接获取父类的成员变量
子父类中成员变量名字是一样的
就近原则,谁离我近我就用谁
继承中成员方法关系(就近原则)
不同名的方法 同名的方法