Animal.java
public abstract class Animal { String color; int age; String sex; abstract void eatThing(); abstract void logThing(); public Animal (int age) { this.age = age; } public Animal () { this.age = 1; } }
Dog.java
package test; public class Dog extends Animal { String loyal; public void eatThing () { System.out.println("eating"); } public void logThing () { System.out.println("color : " + color + ", age : " + age + ", sex : " + sex); } public Dog (int age) { super(age); } public Dog () { super(); } public static void main (String[] args) { Dog d = new Dog(); Animal d2 = new Dog(10); d.logThing(); d2.logThing(); } }
抽象类可以定义构造方法,可以使用父类类型,抽象类没有实例对象不能 new 出来。