继承中的方法重写的注意事项

package org.westos_01_继承;
/*
 * 对于方法重写的注意事项:
 * 1)父类的中的成员方法被private修饰,那么子类不能重写父类中的私有的成员方法
 * 2)如果父类中的成员是一个静态的,那么子类在重写的时候,必须也是静态的成员方法,否则有问题
 * 3)子类的成员方法要重写父类的成员方法,访问权限不能更低,最起码跟父类的权限一致!
 * 
 * */
class Father7{
public void show(){
System.out.println("show father....");
}

//私有的成员方法
private void method(){
System.out.println("method Father...");
}
public static void function(){
System.out.println("function father");
}

public  void function2(){
System.out.println("function2 father");
}
}


class Son7 extends Father7{
//子类要重写父类的方法
public void show(){
System.out.println("show son.....");

}

// public void method(){
// System.out.println("method son....");
// }

public static void function(){
System.out.println("function son....");
}

public void function2(){
System.out.println("function2 son");
}


}
public class ExtendsDemo10 {
public static void main(String[] args) {
//创建子类对象
Son7 s = new Son7() ;
// s.method() ;
s.function() ;
s.function2() ;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值