一、抽象方法
public abstract void eat();
二、抽象类
package cn.wt.day08.Demon12; public abstract class Animal { public abstract void eat(); }
注意·:含有抽象方法的类,一定是抽象类;抽象类不一定含有抽象方法
三、注意事项
1、抽象类不能直接实例化,要通过抽象类的子类实现实例化
2、子类一定要重写(overrider)抽象类的所有抽象方法
例子
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.wt.day08.Demon12; 2 3 public abstract class Animal { 4 public abstract void eat(); 5 public abstract void sleep(); 6 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.wt.day08.Demon12; 2 3 public abstract class Dog extends Animal { 4 @Override 5 public void eat() { 6 System.out.println("狗吃骨头"); 7 } 8 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.wt.day08.Demon12; 2 3 public class TuDog extends Dog{ 4 5 @Override 6 public void sleep() { 7 System.out.println("趴在稻草堆里睡"); 8 } 9 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.wt.day08.Demon12; 2 3 public class Demon { 4 public static void main(String[] args) { 5 TuDog wuMing = new TuDog(); 6 wuMing.eat(); 7 wuMing.sleep(); 8 } 9 }