overriding. want to make a public virtual function private
-
I want to make a function unaccessable in a child class (that's public in the parent class) The parent class has: public virtual void ShowPopup(System.Drawing.Point p) I tried doing this in the child class: private override void ShowPopup(System.Drawing.Point p) The compiler compains that I can't make a virtual function private :( Any ideas how I could hide this function? Thanks, Elena Elena
-
I want to make a function unaccessable in a child class (that's public in the parent class) The parent class has: public virtual void ShowPopup(System.Drawing.Point p) I tried doing this in the child class: private override void ShowPopup(System.Drawing.Point p) The compiler compains that I can't make a virtual function private :( Any ideas how I could hide this function? Thanks, Elena Elena
The compiler is right because
private
means that only the class that has the method/property/field can use it. No other class is permitted access including any class derived from it. Andvirtual
tells the compiler to expect derived classes to override it. Therefore making a methodprivate
andvirtual
would be utterly pointless. Hiding the method kind of goes against the grain of Object Orientation. I remember years ago my OO lecturer at University going on about that. When you create derived classes you are creating specialisations of the base. You should never be taking anything away from the base, just adding new methods and specialising existing ones. I would suggest that you re-examine your class design and work out if there is a way to better design the classes so that you are not having to take away the functionality in the base. Alternatively you could explain a bit about your design and maybe some people here can help you re-work it. --Colin Mackay--"In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown) Enumerators in .NET: See how to customise foreach loops with C#
-
I want to make a function unaccessable in a child class (that's public in the parent class) The parent class has: public virtual void ShowPopup(System.Drawing.Point p) I tried doing this in the child class: private override void ShowPopup(System.Drawing.Point p) The compiler compains that I can't make a virtual function private :( Any ideas how I could hide this function? Thanks, Elena Elena
You can't make a public function in a parent class private in a child child class. if you could, it would break polymorphism since the inherited class would not support the parent's "interface" or contract. What you can do is override ShowPopup so that is does nothing.
-
I want to make a function unaccessable in a child class (that's public in the parent class) The parent class has: public virtual void ShowPopup(System.Drawing.Point p) I tried doing this in the child class: private override void ShowPopup(System.Drawing.Point p) The compiler compains that I can't make a virtual function private :( Any ideas how I could hide this function? Thanks, Elena Elena
-
You can't make a public function in a parent class private in a child child class. if you could, it would break polymorphism since the inherited class would not support the parent's "interface" or contract. What you can do is override ShowPopup so that is does nothing.
Mailing Lists wrote: What you can do is override ShowPopup so that is does nothing. You could do. But that sounds really messy and horrible because it could be that some time later a developer is doing something and expecting a pop up and then spending ages trying to work out why a popup wasn't happening. Then I'd get really :mad:. --Colin Mackay--
"In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown) Enumerators in .NET: See how to customise foreach loops with C#
-
The compiler is right because
private
means that only the class that has the method/property/field can use it. No other class is permitted access including any class derived from it. Andvirtual
tells the compiler to expect derived classes to override it. Therefore making a methodprivate
andvirtual
would be utterly pointless. Hiding the method kind of goes against the grain of Object Orientation. I remember years ago my OO lecturer at University going on about that. When you create derived classes you are creating specialisations of the base. You should never be taking anything away from the base, just adding new methods and specialising existing ones. I would suggest that you re-examine your class design and work out if there is a way to better design the classes so that you are not having to take away the functionality in the base. Alternatively you could explain a bit about your design and maybe some people here can help you re-work it. --Colin Mackay--"In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown) Enumerators in .NET: See how to customise foreach loops with C#
Thank you for your replies. The reason why I want to hide the parent class is because I need more data for the child. By the way the parents class is built in, I can't change it: DevExpress.XtraBars.PopupMenu I want the user to call the following function that is implemented by the child public void ShowPopup(System.Drawing.Point p, File selectedFile, Folder selectedFolder, Folder destinationFolder) instead of the original public void ShowPopup(System.Drawing.Point p) I could have two separate calls: SetFileData(selectedFile, selectedFolder, destinationFolder) and then ShowPopup(p), but there will be big bad problems if the user forgets to make the first call. Elena
-
Thank you for your replies. The reason why I want to hide the parent class is because I need more data for the child. By the way the parents class is built in, I can't change it: DevExpress.XtraBars.PopupMenu I want the user to call the following function that is implemented by the child public void ShowPopup(System.Drawing.Point p, File selectedFile, Folder selectedFolder, Folder destinationFolder) instead of the original public void ShowPopup(System.Drawing.Point p) I could have two separate calls: SetFileData(selectedFile, selectedFolder, destinationFolder) and then ShowPopup(p), but there will be big bad problems if the user forgets to make the first call. Elena
I assume by "user" in this case you mean another "developer". To avoid confustion, I'll use the term "developer" to mean a person that writes code, and "user" to mean a person that uses software applications. Okay, so you have a third party component that exposes a method called ShowPopup(), you want to derive a class from it and extend the functionality by implementing your own ShowPopup with extra parameters. In the new derived class the base.ShowPopup() will work correctly so long as the developer has called SetFileDate(). Right? My advice is to not mollycoddle other developers, defend against users doing weird things but not developers. Document that there is an extended version of ShowPopup() with the extra parameters which is to be used in preference for this class. Also document that if the original ShowPopup is used that the SetFileData() must be called first for this class. Anything else is wrong and will cause an exception to be thrown (remember that is what exceptions are for). Here's an example of something designed in a similar way to what I've described: What happens if you have an array with 5 elements and you try and get the 10th element? An exception is thrown. Why? Because that was a dumb thing to do. If it helps, you should override the ShowPopup() [You mentioned it was virtual already] and check for the prensence of the file data and throw the relevant exception if it doesn't exist. (If you don't have a relevant exception, create a new exception class) Begin Rambling Nostalgia: I remember back in the days where I did C++ development the documentation was full of remarks like "The first parameter takes an integer between 1 and 12, for any other supplied value the results are undefined". In other words, "Garbage In, Garbage Out". In reality the "undefined" results could mean anything from invalid calculations to the whole application crashing. At least now there are exceptions so that invalid/missing inputs or expected preconditions can be thrown out and dealt with properly. I hope this helps. --Colin Mackay--
"In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown) Enumerators in .NET: See how to customise foreach loops with C#
-
Mailing Lists wrote: What you can do is override ShowPopup so that is does nothing. You could do. But that sounds really messy and horrible because it could be that some time later a developer is doing something and expecting a pop up and then spending ages trying to work out why a popup wasn't happening. Then I'd get really :mad:. --Colin Mackay--
"In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown) Enumerators in .NET: See how to customise foreach loops with C#
I would override the method and throw an
NotSupportedException
. And document that the method is not supported. This keeps the developers out of confusion. Maybe like this:throw new NotSupportedException("This method is not supported by this class.
Use the overload ShowPopup(Point, File, Folder, Folder) instead.")