absract关键字创建抽象类,抽象类不会创建对象。
抽象类中只声明而不去具体实现的方法称为抽象方法,要用abstract关键字去修饰。
public abstract class Fruit {//抽象类 //声明抽象方法 public abstract void harvest(); }
public class Apple extends Fruit{//创建子类Apple public void harvest() {//实现harvest方法 System.out.println("苹果已经收获!"); } }
public class Orange extends Fruit{//创建子类Orange public void harvest() {//实现harvest方法 System.out.println("桔子已经收获!"); } }
public class Farm { public static void main(String[] args) { Apple apple=new Apple(); apple.harvest(); Orange orange=new Orange(); orange.harvest(); } }