Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7667
| From | Ethan <sam_cit@yahoo.co.in> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Avoid derived class to override a method yet should allow callers to invoke the method |
| Date | 2011-09-06 22:40 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <1e0a57fd-cd79-45d8-ab37-252b981e3dfe@s2g2000vby.googlegroups.com> (permalink) |
| References | <79cc1af8-1f3c-41c7-bbe2-8c5085b25e7c@t9g2000yqi.googlegroups.com> <38560bc6-af42-471f-b750-d2966452b1c3@glegroupsg2000goo.googlegroups.com> |
On Sep 6, 9:40 pm, Lew <lewbl...@gmail.com> wrote:
> Ethan wrote:
> > I have a requirement where base class has method (implemented) and
> > 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.
>
> > Is this possible?
>
> Absolutely.
>
> You prevent an override by 'final' in the method signature, and this is very frequently The Right Thing To Do.
>
> You make the method callable by client code through 'public' in the method signature, which is the standard thing to do.
>
> public class BaseOfOperations
> {
> public final void DoSomething()
> {
> // implementation here
> }
>
> }
>
> public class SpecificOperations extends BaseOfOperations
> {
> // cannot override DoSomething()
>
> }
>
> public class Client
> {
> public void Whatever()
> {
> BaseOfOperations boo = new SpecificOperations();
> boo.DoSomething();
> }
>
> }
>
> 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.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Avoid derived class to override a method yet should allow callers to invoke the method Ethan <sam_cit@yahoo.co.in> - 2011-09-06 21:05 -0700
Re: Avoid derived class to override a method yet should allow callers to invoke the method Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-09-06 21:38 -0700
Re: Avoid derived class to override a method yet should allow callers to invoke the method Lew <lewbloch@gmail.com> - 2011-09-06 21:40 -0700
Re: Avoid derived class to override a method yet should allow callers to invoke the method Ethan <sam_cit@yahoo.co.in> - 2011-09-06 22:40 -0700
Re: Avoid derived class to override a method yet should allow callers to invoke the method Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-09-07 10:35 +0000
Re: Avoid derived class to override a method yet should allow callers to invoke the method Lew <lewbloch@gmail.com> - 2011-09-07 08:14 -0700
csiph-web