Visual Basic .NET » Visual Basic .NET General Discussion
Inheriting Classes -- Joe Enos --


I am working on user controls and other inherited classes. Is there any way to remove a method from the base class, so that the user cannot select it? I can potentially override that method and automatically throw an Exception, but I would prefer that the user not see it at all.

Is this possible?

Thanks

Joe Enos

-- Sankalp --


I am not sure I get the question right, but AFAIK, if you have some function say fnA in the base class BC and another derived class DC inherits BC, and you dont want DC to be able to access fnA, then you can specify fnA as private. Only protected functions (or public ones)
can be accessed from a derived class.

~Sankalp

-- pavan kumar --

what is the use if generics in C#
what about yield, where,parcial and nullable cany any one explain in deep

-- "Bradley, Peter" --


Your question is not entirely clear. If you mean that none of the sub-classes should inherit this method, then you have your answer already - which is to make the method private.

There is, however, another scenario that is usually termed, "Inheritance for Limitation". In this case, the child, "... restricts the use of some behaviour inherited from the parent class" (Timothy Budd,
"Understanding Object Oriented Programming with Java" (2002) Pearson Education).

I don't know of any direct implementation of this concept in C# or,
indeed, in any other language. The problem is that it breaks the principle of substitutability - the principle that states that a sub-class "is-a" super-class. Most texts therefore recommend that this type of inheritance be avoided.

If you really must use Inheritance for Limitation then, as you say, you can make the sub-class generate an exception if the method is called; or you can make it print an error message. And, as you rightly imply,
neither of these is particularly elegant.

A better method might be to analyse why you are inheriting from the super-class in this case. Does this sub-class object use methods inherited from the super-class, either by not over-riding them, or by over-riding them whilst calling the super-class method in the over-riding method? Or does it simply always over-ride the super-class methods without calling them anywhere in the over-riding methods? If the latter is the case, your inheritance (for this particular child) is an example of "Inheritance for Specification", i.e. interface inheritance. Assuming this to be so, you can define an interface that specifies all the methods you want to use, but not the one(s) that you don't. Your super-class and your sub-class can then both implement this interface.

Your sub-class will simply implement the interface and will not extend the super-class at all. All your other child classes can continue to extend the super-class (without implementing the interface, since the super-class has already implemented it).

HTH
Peter
-----Original Message-----
From: DotNetDevelopment
Inheriting Classes
I am working on user controls and other inherited classes. Is there any way to remove a method from the base class, so that the user cannot select it? I can potentially override that method and automatically throw an Exception, but I would prefer that the user not see it at all.

Is this possible?

Thanks

Joe Enos


-- Joe Enos --


I think you understand what I'm looking for, but the compiler won't let me create the method as private or protected. If the method of the base class is public, then it won't compile unless the override method is also public.

Any other ideas on how I can "cancel" out methods from the base class?

Thanks

Joe

-- Joe Enos --


Peter:
Thank you for the explanation. I believe that the interface methodology is probably what I'm looking for.

I appreciate the help.

Joe

-- "Bradley, Peter" --


It's the base class method that should be private. This will prevent any sub-class from over-riding it (I think ...) or calling it
(definitely).

This may, or may not be what you want.
Peter

-----Original Message-----
From: DotNetDevelopment
Re: Inheriting Classes
I think you understand what I'm looking for, but the compiler won't let me create the method as private or protected. If the method of the base class is public, then it won't compile unless the override method is also public.

Any other ideas on how I can "cancel" out methods from the base class?

Thanks

Joe


-- Joe Enos --


I'm actually inheriting from the .NET framework, so I don't have any control over the base class. But I'll note that for when I inherit from my own classes.

Thanks

[Submit Comment]Home