子類
public class Son extends Father{
@Override
//如果沒有重寫showAge方法,系統會報錯,要麼將Son類設置爲抽象類,要麼對ShowAge方法進行重寫,實現功能。
public int showAge() {
// TODO Auto-generated method stub
Son son = new Son();
son.setAge(18);
return son.getAge();
}
//抽象類不能進行實例化,(但是可以保留構造方法,以備子類調用)
//Cannot instantiate the type Father
// Father father = new Father();
}
測試
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Son son = new Son();
System.out.println(son.showAge());
}
}
父類-抽象類
public abstract class Father {
int age=50;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Father(int age) {
super();
this.age = age;
}
public Father() {
super();
}
//抽象方法沒有方法體,只是聲明有這個方法,並且非抽象子類必須重寫此方法(也就是必須用這個方法,來實現功能
public abstract int showAge();
}