Is it possible to hide a method from inheriting from a parent class ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmallyakoley
    New Member
    • Nov 2009
    • 7

    Is it possible to hide a method from inheriting from a parent class ?

    Suppose I have a class

    Code:
    class A
      {
        public void Foo()
        {
    
         }
    }
    i want to inherit A to class B & C

    Code:
    class B:A
      {
    
      }
    
    class C:A
    {
    
    }
    How can I write the class A so that I can access Foo() by object of class B but by C object Foo() will be unavailable.Is it possible?.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      I don't think you can do what you want. I think the best you can hope for is to make the class a sealed class so that it can't be inherited. Here, have a look at this link, hopefully it explains things better than I can, inheritance has never been a strong suit of mine.

      Comment

      • nirmallyakoley
        New Member
        • Nov 2009
        • 7

        #4
        Dear Gary,
        Thank you so much for your kind respone.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Quote MSDN:
          The sealed modifier can be applied to classes, instance methods and properties. A sealed class cannot be inherited. A sealed method overrides a method in a base class, but itself cannot be overridden further in any derived class. When applied to a method or property, the sealed modifier must always be used with override (C# Reference).
          Says you can seal an individual method, which might work for you.


          Your other option might be to mark things internal. Which means only classes in the same assembly can use it. So if you are say compiling a DLL with a base class and a class deriving from it. Only the deriving classes in your DLL can use the function. At least that's how I've come to understand it.

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            This is why I love this forum, you learn something new almost every day. Nice work, Plater! :)

            Comment

            Working...