抽象方法不能使用private修饰符,也不宜使用默认修饰符(default)
(1)如果使用private修饰符
public abstract class SuperClass {
/*
* The abstract method test in type SuperClass can only set a visibility modifier,
* one of public or protected
*/
private abstract void test();//编译通不过
}
抽象方法必须对子类可见,而private修饰的方法对子类不可见,因此不能使用private修饰符。
(2)如果使用默认修饰符(即不带修饰符)
package cn;
public abstract class SuperClass {
//采用package修饰符,可以通过编译,但是后患无穷
//该方法,不一定对子类可见,除非子类也在这个包下
abstract void test();
}
package com;
import cn.SuperClass;
public class SubClass extends SuperClass {
/*
* This class must implement the inherited abstract method SuperClass.test(),
* but cannot override it since it is not visible from SubClass.
* Either make the type abstract or make the inherited method visible
*/
@Override
void test() {//编译出错
//子类和父类,不在同一个包下,因此不能访问父类的package方法
}
}