Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!news2.arglkargh.de!news.mixmin.net!aioe.org!.POSTED!not-for-mail From: Xue Feng Newsgroups: comp.lang.java.programmer Subject: overriding with different return value type Date: Thu, 16 Jul 2015 21:04:44 +0800 Organization: Aioe.org NNTP Server Lines: 45 Message-ID: NNTP-Posting-Host: y2uWWSnebCe98gBeFa8mIQ.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 X-Notice: Filtered by postfilter v. 0.8.2 X-Mozilla-News-Host: news://nntp.aioe.org:119 Xref: csiph.com comp.lang.java.programmer:32042 According to the JDK manual, overriding requirs that (1) the same signature (2) the same return value type The following example demonstrates it: class Base{ public int sum(int x, int y){ System.out.println("Int"); return x + y; } } class Ext extends Base{ public double sum(int x, int y){ System.out.println("Double"); return (double)(x + y); } } The overriding sum in Ext returns double, while the overrided sum in Base returns int. Their return value type are different. So, we get error message when compiling. But the other example seems work in different way. The Object class has a clone() method, and its header is like this: protected Object clone() throws CloneNotSupportedException The clone() in Object class returns an Object's instance. Suppose that there is a class implements cloneable interface: class Foobar implements Cloneable{ public Foobar clone() throws CloneNotSupportedException{ System.out.println("Foobar"); return (Foobar)super.clone(); } } The clone() in Foobar class returns an Foobar's instance. It's obvious that the two clone() methods have different return value type, but no error message comes while compiling. I wonder why the clone() method works in different way.