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. Preventing method access in derived class.

Preventing method access in derived class.

Scheduled Pinned Locked Moved C#
question
10 Posts 6 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.
  • D Offline
    D Offline
    Diana Fernandez
    wrote on last edited by
    #1

    Hi Is it possible to prevent accessing a method in derived class? Since "sealed" keyword is only used along with "override" I can not use this option in the base class. Thanks, Diana.

    L C S D 4 Replies Last reply
    0
    • D Diana Fernandez

      Hi Is it possible to prevent accessing a method in derived class? Since "sealed" keyword is only used along with "override" I can not use this option in the base class. Thanks, Diana.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      private methods cannot be overridden.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      1 Reply Last reply
      0
      • D Diana Fernandez

        Hi Is it possible to prevent accessing a method in derived class? Since "sealed" keyword is only used along with "override" I can not use this option in the base class. Thanks, Diana.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        how about Private ?

        1 Reply Last reply
        0
        • D Diana Fernandez

          Hi Is it possible to prevent accessing a method in derived class? Since "sealed" keyword is only used along with "override" I can not use this option in the base class. Thanks, Diana.

          S Offline
          S Offline
          Sathesh Sakthivel
          wrote on last edited by
          #4

          Diana Fernandez wrote:

          Is it possible to prevent accessing a method in derived class?

          Yes. Go with Private Access Modifier.

          Regards, Satips.:rose:

          1 Reply Last reply
          0
          • D Diana Fernandez

            Hi Is it possible to prevent accessing a method in derived class? Since "sealed" keyword is only used along with "override" I can not use this option in the base class. Thanks, Diana.

            D Offline
            D Offline
            Diana Fernandez
            wrote on last edited by
            #5

            Consider the scenario given below public class Base { protected string _name = null; public void SetName(string Name) { _name = Name; } } public class Derived : Base { public void SetName(string Name, bool Condition) { if (Condition) _name = Name + "true"; else _name = Name + "false"; } } How is it possible to prevent accessing the Base class method on the object of derived class? Because base class implementation should not be used on the object of derived class.

            M C 2 Replies Last reply
            0
            • D Diana Fernandez

              Consider the scenario given below public class Base { protected string _name = null; public void SetName(string Name) { _name = Name; } } public class Derived : Base { public void SetName(string Name, bool Condition) { if (Condition) _name = Name + "true"; else _name = Name + "false"; } } How is it possible to prevent accessing the Base class method on the object of derived class? Because base class implementation should not be used on the object of derived class.

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              Hello, in this case you should declare it as virtual und override method.

              public class Base
              {
              protected string _name = null;

              protected virtual void SetName(string Name)
              {
                  \_name = Name;
              }
              

              }

              public class Derived : Base
              {
              protected override void SetName(string Name, bool Condition)
              {
              if (Condition)
              _name = Name + "true";
              else
              _name = Name + "false";
              }
              }

              All the best Martin

              C 1 Reply Last reply
              0
              • M Martin 0

                Hello, in this case you should declare it as virtual und override method.

                public class Base
                {
                protected string _name = null;

                protected virtual void SetName(string Name)
                {
                    \_name = Name;
                }
                

                }

                public class Derived : Base
                {
                protected override void SetName(string Name, bool Condition)
                {
                if (Condition)
                _name = Name + "true";
                else
                _name = Name + "false";
                }
                }

                All the best Martin

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                That will not compile, the method signatures differ.

                Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                M 1 Reply Last reply
                0
                • D Diana Fernandez

                  Consider the scenario given below public class Base { protected string _name = null; public void SetName(string Name) { _name = Name; } } public class Derived : Base { public void SetName(string Name, bool Condition) { if (Condition) _name = Name + "true"; else _name = Name + "false"; } } How is it possible to prevent accessing the Base class method on the object of derived class? Because base class implementation should not be used on the object of derived class.

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  Diana Fernandez wrote:

                  Because base class implementation should not be used on the object of derived class.

                  It's a feature of OO ( and the whole point of inheritance ) that derived classes DO inherit the base methods. The best you could do is make SetName virtual on the base class, and override it on the derived, so it calls the new method with a default condition, or have it throw an error. public override SetName(string Name) { SetName(name, true); }

                  Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                  U 1 Reply Last reply
                  0
                  • C Christian Graus

                    That will not compile, the method signatures differ.

                    Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                    M Offline
                    M Offline
                    Martin 0
                    wrote on last edited by
                    #9

                    Uuupss, Haven't seen that. than the 'new' would be best todo I think. public new void SetName(string Name, bool Condition) { if (Condition) _name = Name + "true"; else _name = Name + "false"; } } All the best, Martin

                    1 Reply Last reply
                    0
                    • C Christian Graus

                      Diana Fernandez wrote:

                      Because base class implementation should not be used on the object of derived class.

                      It's a feature of OO ( and the whole point of inheritance ) that derived classes DO inherit the base methods. The best you could do is make SetName virtual on the base class, and override it on the derived, so it calls the new method with a default condition, or have it throw an error. public override SetName(string Name) { SetName(name, true); }

                      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                      U Offline
                      U Offline
                      Urs Enzler
                      wrote on last edited by
                      #10

                      Or use aggregation instead of inheritance - maybe in combination with an interface.

                      -^-^-^-^-^- no risk no funk

                      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