Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!novia!news-out.readnews.com!transit4.readnews.com!postnews.google.com!s2g2000vby.googlegroups.com!not-for-mail From: Ethan Newsgroups: comp.lang.java.programmer Subject: Re: Avoid derived class to override a method yet should allow callers to invoke the method Date: Tue, 6 Sep 2011 22:40:22 -0700 (PDT) Organization: http://groups.google.com Lines: 51 Message-ID: <1e0a57fd-cd79-45d8-ab37-252b981e3dfe@s2g2000vby.googlegroups.com> References: <79cc1af8-1f3c-41c7-bbe2-8c5085b25e7c@t9g2000yqi.googlegroups.com> <38560bc6-af42-471f-b750-d2966452b1c3@glegroupsg2000goo.googlegroups.com> NNTP-Posting-Host: 75.62.204.165 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1315374823 6050 127.0.0.1 (7 Sep 2011 05:53:43 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 7 Sep 2011 05:53:43 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: s2g2000vby.googlegroups.com; posting-host=75.62.204.165; posting-account=_beFiwoAAAA_k1fgJtZfPVQP5wnC7Wyf User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HNKRUAELSC X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7667 On Sep 6, 9:40=A0pm, Lew wrote: > Ethan wrote: > > =A0 =A0I have a requirement where base class has method (implemented) a= nd > > derived class shouldn't be able to override it. > > However, the method needs to be invoked by a caller who creates an > > instance of the derived class. > > > =A0 Is this possible? > > Absolutely. > > You prevent an override by 'final' in the method signature, and this is v= ery frequently The Right Thing To Do. =A0 > > You make the method callable by client code through 'public' in the metho= d signature, which is the standard thing to do. > > public class BaseOfOperations > { > =A0 public final void DoSomething() > =A0 { > =A0 =A0 // implementation here > =A0 } > > } > > public class SpecificOperations extends BaseOfOperations > { > =A0 // cannot override DoSomething() > > } > > public class Client > { > =A0 public void Whatever() > =A0 { > =A0 =A0 BaseOfOperations boo =3D new SpecificOperations(); > =A0 =A0 boo.DoSomething(); > =A0 } > > } > > I suggest that you read the Java tutorials and a good basic book on Java = programming.http://download.oracle.com/javase/tutorial/ > > -- > Lew Thanks Lew and Peter. Its been a while that i have coded in java and didn't knew much about final.