Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!news.netfront.net!not-for-mail From: Wanja Gayk Newsgroups: comp.lang.java.programmer Subject: Re: Abstract Class versus an Interface, when no Members in Common Date: Tue, 8 Nov 2011 23:13:41 +0100 Organization: Netfront http://www.netfront.net/ Lines: 80 Message-ID: References: <22857359-211e-443e-9c5d-6cc2f5bd971b@m19g2000vbm.googlegroups.com> NNTP-Posting-Host: 77.8.33.249 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: adenine.netfront.net 1320790418 362 77.8.33.249 (8 Nov 2011 22:13:38 GMT) X-Complaints-To: news@netfront.net NNTP-Posting-Date: Tue, 8 Nov 2011 22:13:38 +0000 (UTC) User-Agent: MicroPlanet-Gravity/3.0.4 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9788 In article <22857359-211e-443e-9c5d- 6cc2f5bd971b@m19g2000vbm.googlegroups.com>, kvnsmnsn@hotmail.com says... > > I have a method that needs to be able to return either of two very > different types of data, in one case a class consisting of a two- > dimensional array of s accompanied by a single value, and > in the other case just a one-dimensional array of s 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 . I guess generics could be the remedy to your Problem. Let me sketch something like this. Say we have too classes "Foo" and "Bar", both have nothing in commmon. However, we'd like to have a common interface to obtain both the very special result of our query and some common information, say about our readLimit and if it was exceeded. That could look like this: interface SearchResult{ Iterable items(); ReadLimit readLimit(); } interface ReadLimit{ int maxItems(); boolean isExceeded(); } We then have some consumers, that call some finder to obtain a searchResult and get the totally different types from it, whilst also using the common information: class FooConsumer{ void doSomething(){ SearchResult result = finder.find(Foo.class, new BlueFooPredicate(),new ReadLimit(1000)); for(Foo foo : result.items()){ assert Color.Blue.equals(foo.getColor()); } if(result.readLimit().isExceeded()){ alert("More than "+result.readLimit().maxItems()+" entries found!"); } } } class BarConsumer{ //implementation perhaps injected by a DI-Framework private Finder finder; void doSomething(){ SearchResult result = finder.find(Bar.class, new SteelBarPredicate(), new ReadLimit(500)); for(Bar bar : result.items()){ assert Material.Steel.equals(bar.getMaterial()); } if(result.readLimit().isExceeded()){ alert("More than "+result.readLimit().maxItems()+" entries found!"); } } } Kind regards, Wanja -- ..Alesi's problem was that the back of the car was jumping up and down dangerously - and I can assure you from having been teammate to Jean Alesi and knowing what kind of cars that he can pull up with, when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer] --- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---