How to Stop Derived Class from overriding base class method 
Author Message
 How to Stop Derived Class from overriding base class method

Well, overriding is not the right term I think, but I have a class A that
clients will derive from.  It has a public method [ Public void Foo() ]
that the clients can use.  How do I stop them from using [public new void
Foo() ], or Shadow in VB, in the derived class?  I have other code that will
call the Foo Method from the derived class and want to make sure that the
original method is the only one that is called.

Thanks



Sun, 24 Oct 2004 22:05:34 GMT  
 How to Stop Derived Class from overriding base class method
Bear,

    You can't keep people from declaring a function in that manner.  What
you can do, however, is in your functions, use the base keyword to make sure
you are calling the base class' implementation.  So instead of doing this:

Foo();

    You would do this:

base.Foo();

    This will insure that the correct one is called.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Well, overriding is not the right term I think, but I have a class A that
> clients will derive from.  It has a public method [ Public void Foo() ]
> that the clients can use.  How do I stop them from using [public new void
> Foo() ], or Shadow in VB, in the derived class?  I have other code that
will
> call the Foo Method from the derived class and want to make sure that the
> original method is the only one that is called.

> Thanks



Sun, 24 Oct 2004 22:24:13 GMT  
 How to Stop Derived Class from overriding base class method
Thanks, I did not think there was a lanquage way to do it, but thought there
'might' be some kind of attribute tucked away somewhre that would tell the
compiler not to allow it. That might be a good idea.



Quote:
> Bear,

>     You can't keep people from declaring a function in that manner.  What
> you can do, however, is in your functions, use the base keyword to make
sure
> you are calling the base class' implementation.  So instead of doing this:

> Foo();

>     You would do this:

> base.Foo();

>     This will insure that the correct one is called.

>     Hope this helps.

> --
>                - Nicholas Paldino [.NET MVP]



> > Well, overriding is not the right term I think, but I have a class A
that
> > clients will derive from.  It has a public method [ Public void Foo() ]
> > that the clients can use.  How do I stop them from using [public new
void
> > Foo() ], or Shadow in VB, in the derived class?  I have other code that
> will
> > call the Foo Method from the derived class and want to make sure that
the
> > original method is the only one that is called.

> > Thanks



Sun, 24 Oct 2004 22:57:52 GMT  
 How to Stop Derived Class from overriding base class method
try

public sealed void Foo()


Quote:
> Well, overriding is not the right term I think, but I have a class A that
> clients will derive from.  It has a public method [ Public void Foo() ]
> that the clients can use.  How do I stop them from using [public new void
> Foo() ], or Shadow in VB, in the derived class?  I have other code that
will
> call the Foo Method from the derived class and want to make sure that the
> original method is the only one that is called.

> Thanks



Mon, 25 Oct 2004 04:12:44 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to Create CCtrlView derived class based on CTreeCtrl derived class

2. How to Create CCtrlView derived class based on CTreeCtrl derived class

3. Prevent overriding method in abstract base class ?

4. Inheritance: overriding base-class methods

5. Derived class "hiding" virtual Base method

6. creating an instance of a derived class from a base class

7. Deriving from CControlBar - no base class in New Class dialog

8. How to prevent derived classes from changing a base class function

9. address of base class and derived class different

10. Serialize class derived from virtual base class

11. How do I cast base class to a derived class

12. CDialog derived class as a base class for another dialog box

 

 
Powered by phpBB® Forum Software