抽象类必须有抽象方法吗?

下图程序中空(1) 答案是填 abstract double getGPA()答案本身是没错的,问题是这里有没有必要加这一句,定义这么一个抽象的getGPA方法我自己写了个类似的代码试了一下(在下面),发现去掉这一句照样可以正常运行。这里定义一个抽象方法,我怀疑是不是这样一来,子类就被迫必须实现这个抽象方法了

package pe201301;

abstract class Student{
protected String name;
protected int stuNo;
protected double GPA;
protected int grades;
public Student(int stuNo, String name, int grades){
this.stuNo = stuNo;
this.name = name;
this.grades = grades;
}
//abstract double getGPA();
double computeWg(){
return grades*100;
}
}

class ActStudent extends Student{
private int Apoints;
ActStudent(int stuNo, String name, int grades, int Apoints){
super(stuNo,name,grades);
this.Apoints = Apoints;
}
public double getGPA(){
return GPA = grades*100;
}
}

public class Java {
public static void main(String[] args) {
ActStudent a = new ActStudent(101,"zhang",89,10);
System.out.println(a.getGPA());

}
}

抽象类中并不一定有抽象方法。
如果你的main方法中变为这样
Student a = new ActStudent(101,"zhang",89,10);
System.out.println(a.getGPA());
那么Student中必须定义一个getGPA()方法,不管是不是抽象的
温馨提示:答案为网友推荐,仅供参考
相似回答