Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Avoid derived class to override a method yet should allow callers toinvoke the method

Reply
Thread Tools

Avoid derived class to override a method yet should allow callers toinvoke the method

 
 
Ethan
Guest
Posts: n/a
 
      09-07-2011
Hello,

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?
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      09-07-2011
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
 
Reply With Quote
 
 
 
 
Ethan
Guest
Posts: n/a
 
      09-07-2011
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.
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      09-07-2011
Lew <> wrote:
> public class BaseOfOperations {
> public final void DoSomething()
> public class Client {
> public void Whatever()


Method names should not begin with capital letters.
Not even in trivial example snippets.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-07-2011
On Wednesday, September 7, 2011 3:35:00 AM UTC-7, Andreas Leitgeb wrote:
> Lew <lewb...@gmail.com> wrote:
> > public class BaseOfOperations {
> > public final void DoSomething()
> > public class Client {
> > public void Whatever()

>
> Method names should not begin with capital letters.
> Not even in trivial example snippets.


Oops. Good catch.

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IIS7 error: assembly does not allow partially trusted callers ASF ASP .Net 1 01-13-2009 03:02 AM
Debug That assembly does not allow partially trusted callers Joris van Lier ASP .Net 3 07-08-2008 01:45 AM
That assembly does not allow partially trusted callers =?Utf-8?B?TWFydHluIEZld3RyZWxs?= ASP .Net 5 05-08-2007 01:21 AM
Please help with SecurityException: That assembly does not allow partially trusted callers. Matt Culbreth ASP .Net 1 03-29-2007 07:46 PM
Assembly does not allow partially trusted callers g3000 ASP .Net 4 01-19-2006 04:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57