我们需要知道,由于为抽象类创建对象是不安全的,所以我们会从编译器那里得到一条出错消息。这样,编译器会确保抽象类的纯粹性,我们不必担心会误用它。
编译器直接报错提醒。
看一下完整的例子:
abstract class Rodent{ public abstract void play(); } abstract class Mouse extends Rodent{ public abstract void eat(); } class Gerbil extends Mouse{ @Override public void eat() { // TODO Auto-generated method stub System.out.println("eat!"); } @Override public void play() { // TODO Auto-generated method stub System.out.println("play!"); } public void sleep() { System.out.println("sleep!"); } } public class Demo02 { public static void main(String[] args) { Rodent r1 = new Gerbil(); r1.play(); System.out.println("------------"); Mouse m1 = new Gerbil(); m1.eat(); m1.play(); System.out.println("------------"); Gerbil g1 = new Gerbil(); g1.eat(); g1.play(); g1.sleep(); System.out.println("------------"); } }