简体   繁体   中英

Java vs C# method/function return-type overriding?

Is there a difference in the way Java and C# evaluate method/function signatures to perform overriding? In Java two methods in the child and parent, with same name and parameters, but different return types is an error- whereas in C# it compiles fine.

Am I right in thinking C# views this as overloading whereas Java attempts overriding but only allows overriding of same return types?

you got an error in Java because the return type should be compatible with original return type.

the return type is not part of signature. however, when you override a method you should keep the return type compatible, before Java 5 the return type must be identical .

but now (Java 5+) you can return subclass of the original type, this called Covariant return type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM