Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. overriding. want to make a public virtual function private

overriding. want to make a public virtual function private

Scheduled Pinned Locked Moved C#
graphicsquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elena12345
    wrote on last edited by
    #1

    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

    M C L 3 Replies Last reply
    0
    • E elena12345

      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

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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. And virtual tells the compiler to expect derived classes to override it. Therefore making a method private and virtual 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#

      E 1 Reply Last reply
      0
      • E elena12345

        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

        M Offline
        M Offline
        Mailing Lists
        wrote on last edited by
        #3

        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.

        C 1 Reply Last reply
        0
        • E elena12345

          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

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          elena12345 wrote: Any ideas how I could hide this function? Encapsulation :) leppie::AllocCPArticle("Zee blog");
          Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

          1 Reply Last reply
          0
          • M Mailing Lists

            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.

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            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#

            U 1 Reply Last reply
            0
            • C Colin Angus Mackay

              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. And virtual tells the compiler to expect derived classes to override it. Therefore making a method private and virtual 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#

              E Offline
              E Offline
              elena12345
              wrote on last edited by
              #6

              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

              C 1 Reply Last reply
              0
              • E elena12345

                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

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                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#

                1 Reply Last reply
                0
                • C Colin Angus Mackay

                  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#

                  U Offline
                  U Offline
                  User 260964
                  wrote on last edited by
                  #8

                  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.")

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups