方法的重写
1、在继承中,也存在着重写的概念,其实就是子类定义了和父类同名的方法
2、定义:
方法名称相同,返回值类型相同,参数也同。
3、重写限制:
被子类重写的方法不能拥有比父类方法更加严格的访问权限
4、访问权限:
private<default<public
示例代码:
public class ExtendsDemo04 {
static class A{
public void tell(){
System.out.println("我是tell方法");
}
private void say(){
}
void print(){
}
}
static class B extends A{
public void tell(){
super.tell();
System.out.println("重写了tell方法");
}
public void print(){
}
}
public static void main(String [] args){
B b=new B();
b.tell();
}
}
Super关键字
强行调用父类方法的执行。