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

Discussion in 'Java' started by Ethan, Sep 7, 2011.

  1. Ethan

    Ethan Guest

    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?
     
    Ethan, Sep 7, 2011
    #1
    1. Advertising

  2. Ethan

    Lew Guest

    Re: Avoid derived class to override a method yet should allow callersto invoke the method

    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
     
    Lew, Sep 7, 2011
    #2
    1. Advertising

  3. Ethan

    Ethan Guest

    Re: Avoid derived class to override a method yet should allow callersto invoke the method

    On Sep 6, 9:40 pm, Lew <> 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.
     
    Ethan, Sep 7, 2011
    #3
  4. Re: Avoid derived class to override a method yet should allowcallers to invoke the method

    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.
     
    Andreas Leitgeb, Sep 7, 2011
    #4
  5. Ethan

    Lew Guest

    Re: Avoid derived class to override a method yet should allow callersto invoke the method

    On Wednesday, September 7, 2011 3:35:00 AM UTC-7, Andreas Leitgeb wrote:
    > 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.


    Oops. Good catch.

    --
    Lew
     
    Lew, Sep 7, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. g3000
    Replies:
    4
    Views:
    20,038
    Scott Allen
    Jan 19, 2006
  2. Matt Culbreth
    Replies:
    1
    Views:
    529
    bruce barker
    Mar 29, 2007
  3. =?Utf-8?B?TWFydHluIEZld3RyZWxs?=

    That assembly does not allow partially trusted callers

    =?Utf-8?B?TWFydHluIEZld3RyZWxs?=, May 2, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    764
    Steven Cheng[MSFT]
    May 8, 2007
  4. Joris van Lier
    Replies:
    3
    Views:
    917
    Alvin Bruney [ASP.NET MVP]
    Jul 8, 2008
  5. ASF
    Replies:
    1
    Views:
    1,497
    Mr. Arnold
    Jan 13, 2009
Loading...

Share This Page