1. class BaseClass { 
2. private float x = 1.of; 
3. protected float getVar() { return x; } 
4. } 
5. class SubClass extends BaseClass { 
6. private float x = 2.Of; 
7. // insert code here 
8. } 
 
Which two are valid examples of method overriding when inserted at line 7? (Choose 
two) 
 
A.  float getVar() { return x; } 
B.  public float getVar() { return x; } 
C.  public double getVar() { return x; } 
D.  protected float getVar() { return x; } 
E.  public float getVar(float f) { return f; } 
 
Answer: B, D which is right?
a not right?

解决方案 »

  1.   

    A 也是正确的,在eclipse里调试通过。
    easy question,100 score?
      

  2.   

    类里面的成员默认权限default,是package访问权限。
    但proctected是same package and subclass(include subclass in other packages),
    所以proctected范围更大,理论上A是错误的。但eclipse为什么可以通过,不太清楚。class BaseClass { 
    private float x = 1.0f; 
    protected float getVar() { return x; } 

    class SubClass extends BaseClass { 
    private float x = 2.0f; 
    float getrVar(){return x;}
    public static void main(String []arg){
    System.out.println("SubClass().getrVar= "+new SubClass().getrVar());
    }

    result:
    SubClass().getrVar= 2.0