Java中抽象类和接口之间的区别
以下是Java接口和抽象类之间的显着区别。
示例
public class Tester { public static void main(String[] args) { Car car = new Car(); car.setFuel(); car.run(); Truck truck = new Truck(); truck.setFuel(); truck.run(); } } interface Vehicle { public void setFuel(); public void run(); } class Car implements Vehicle { public void setFuel() { System.out.println("Car: Tank is full."); } public void run() { System.out.println("Car: Running."); } } abstract class MotorVehicle { public void setFuel() { System.out.println("MotorVehicle: Tank is full."); } abstract public void run(); } class Truck extends MotorVehicle { public void run() { System.out.println("Truck: Running."); } }
输出结果
Car: Tank is full. Car: Running. MotorVehicle: Tank is full. Truck: Running.