java override

java中私有或静态的方法不能被重写java中重写的方法的修饰符只能扩大,而不能缩小(Cannot reduce the visibility of the inherited method from Parent)

重写的方法返回类型不能改变。另外,子类不能定义这样的类:和父类的方法名字相同,参数列表相同,返回类型不同。

public class Parent {
    
    public void print(){
    }
}

public class Test extends Parent{

    @Override
    protected void print(){ //compile error
    }
   
    public int print() { //compile error
return 1;
} }

 

public class Parent {
    
    void print(){  //default: package
    }
}

public class Test extends Parent{

    @Override
    protected void print(){
    }
}

 

posted @ 2015-02-19 18:14  牧 天  阅读(210)  评论(0)    收藏  举报