Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Abstract Class versus an Interface, when no Members in Common Date: Fri, 04 Nov 2011 17:57:06 +0100 Lines: 43 Message-ID: <9hijr4FapeU1@mid.individual.net> References: <22857359-211e-443e-9c5d-6cc2f5bd971b@m19g2000vbm.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net oDjNf6V97zZ/yjLH7hKEjQuCJpqD93ktsQKBjsEWDJU/mDZpY= Cancel-Lock: sha1:wcfXXtVw3UIkWXEPQUADYudN99M= User-Agent: Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 In-Reply-To: <22857359-211e-443e-9c5d-6cc2f5bd971b@m19g2000vbm.googlegroups.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9520 On 11/04/2011 12:09 AM, KevinSimonson wrote: > I have a method that needs to be able to return either of two very > different types of data, Based on what criteria? > in one case a class consisting of a two- > dimensional array ofs accompanied by a single value, and > in the other case just a one-dimensional array ofs and that's > all. So I created an abstract class called that has no > members, not even any constructors, and made two classes extend > , the one class having the two-dimensional array and the > value, and the other having the one-dimensional array. Then I > have my method return a value of. > > My question is, in a situation like this where there are absolutely no > variables or methods the two classes have in common, is it better to > have an abstract class that both classes extend, or is it better to > have an interface that both classes implement? Or is there any > difference between the two approaches? Since your result classes do not have anything in common you could as well use Object as return type. From your description the caller needs to downcast SearchResult anyway. If SearchResult does not have any properties (methods) then the type may be superfluous altogether. There are really only few cases where an interface without methods does make sense (e.g. Serializable and Cloneable as tagging interfaces). In your case the only property that could be associated with the empty interface (or abstract class) SearchResult is that it is returned by your method X. But that is not a property of a type; rather it is an indication of usage. If it's clear at call site which return type you'll get you could have a method with a generic type parameter, e.g. public T search(...) {...} Maybe you even have two search methods with different argument lists each returning a specific type. Kind regards robert