继承中子类重写父类的方法时,可以修改返回值类型吗? java从入门到精通上说可以 但是我Eclipse上编写时子类方法的返回值类型与父类不同时就会报错! 到底是怎样呢?
网上随便找都没有说重写可以修改返回类型的,
你那买的是什么书啊,最好买些比较权威的书籍,
那种(从入门到精通之类的)还是别买了~~~
[quote]A、重写规则之一:重写方法不能比被重写方法限制有更严格的访问级别。
(但是可以更广泛,比如父类方法是包访问权限,子类的重写方法是public访问权限。)
比如:Object类有个toString()方法,开始重写这个方法的时候我们总容易忘记public修饰符,编译器当然不会放过任何教训我们的机会。出错的原因就是:没有加任何访问修饰符的方法具有包访问权限,包访问权限比public当然要严格了,所以编译器会报错的。
B、重写规则之二:参数列表必须与被重写方法的相同。
重写有个孪生的弟弟叫重载,也就是后面要出场的。如果子类方法的参数与父类对应的方法不同,那么就是你认错人了,那是重载,不是重写。
C、重写规则之三:返回类型必须与被重写方法的返回类型相同。
父类方法A:void eat(){} 子类方法B:int eat(){} 两者虽然参数相同,可是返回类型不同,所以不是重写。
父类方法A:int eat(){} 子类方法B:long eat(){} 返回类型虽然兼容父类,但是不同就是不同,所以不是重写。
D、重写规则之四:重写方法不能抛出新的异常或者比被重写方法声明的检查异常更广的检查异常。但是可以抛出更少,更有限或者不抛出异常。
import java.io.*;
public class Test {
public static void main (String[] args) {
Animal h = new Horse();
try {
h.eat();
}
catch (Exception e) {
}
}
}
class Animal {
public void eat() throws Exception{
System.out.println ("Animal is eating.");
throw new Exception();
}
}
class Horse extends Animal{
public void eat() throws IOException{
System.out.println ("Horse is eating.");
throw new IOException();
}
}
这个例子中,父类抛出了检查异常Exception,子类抛出的IOException是Exception的子类,也即是比被重写的方法抛出了更有限的异常,这是可以的。如果反过来,父类抛出IOException,子类抛出更为宽泛的Exception,那么不会通过编译的。
注意:这种限制只是针对检查异常,至于运行时异常RuntimeException及其子类不再这个限制之中。
E、重写规则之五:不能重写被标识为final的方法。
F、重写规则之六:如果一个方法不能被继承,则不能重写它。
比较典型的就是父类的private方法。下例会产生一个有趣的现象。
public class Test {
public static void main (String[] args) {
//Animal h = new Horse();
Horse h = new Horse();
h.eat();
}
}
class Animal {
private void eat(){
System.out.println ("Animal is eating.");
}
}
class Horse extends Animal{
public void eat(){
System.out.println ("Horse is eating.");
}
}
这段代码是能通过编译的。表面上看来违反了第六条规则,但实际上那是一点巧合。Animal类的eat()方法不能被继承,因此Horse类中的eat()方法是一个全新的方法,不是重写也不是重载,只是一个只属于Horse类的全新的方法!这点让很多人迷惑了,但是也不是那么难以理解。
main()方法如果是这样:
Animal h = new Horse();
//Horse h = new Horse();
h.eat();
编译器会报错,为什么呢?Horse类的eat()方法是public的啊!应该可以调用啊!请牢记,多态只看父类引用的方法,而不看子类对象的方法![/quote]
Overriding and overloading are common concepts of Java and occur often on SCJP exam. Although I know the rules that apply for them, I happen to think twice (or more) on questions dealing with them. Thus, it’s a good idea to keep the following rules in mind…
Overriding
applies ONLY to inherited methods
is related to polymorphism
object type (NOT reference variable type) determines which overriden method will be used at runtime
overriding method MUST have the same argument list (if not, it might be a case of overloading)
overriding method MUST have the same return type; the exception is covariant return (used as of Java 5) which returns a type that is a subclass of what is returned by the overriden method
overriding method MUST NOT have more restrictive access modifier, but MAY have less restrictive one
overriding method MUST NOT throw new or broader checked exceptions, but MAY throw fewer or narrower checked exceptions or any unchecked exceptions
abstract methods MUST be overridden
final methods CANNOT be overridden
static methods CANNOT be overridden
constructors CANNOT be overridden
Overloading
overloading can take place in the same class or in the subclass
overloaded methods MUST have a different argument list
overloaded methods MAY change the return type (in case argument list is different)
overloaded methods MAY change the access modifier
overloaded methods MAY throw new or broader checked excpetions
reference type determines which overloaded method will be used at compile time
constructors MAY be overloaded
methods adjustment in connection with overloaded method’s arguments:
you cannot widen and then box (int -> Long)
you can box and then widen (int -> Object, via Integer)
you can combine var args with either widening (byte -> int) or boxing (int -> Integer):
widening is over boxing
widening is over var args
boxing is over var args
Finally, a few notes on polymorphism:
a refenrence variable is of an unchangeable type, but can refer to a subtype object
a single object can be referred to by reference variable of many differnet types (however, they MUST be the of same type or supertype of the object)
reference type determines which method will be called
楼上说得非常清楚了,盗版书籍害人呀!在Java里面封装,继承,多态!其中多态具体表现为重写和重载;
重写是发生在有继承关系的父子类关系里面,规则第一,子类的访问修饰符不能低于父类,返回类型必须一样,方法名一样,参数列表一样!还有就是异常集体的google一下很多的!
建议以后买正版一点的书吧!