locked
how to access sealed class public method in to othere classes RRS feed

  • Question

  • Hi,

    how to access sealed class public method in to othere classes

    Regards,

    Suresh

    Thursday, June 16, 2011 1:24 PM

Answers

  • You can't.

    When a class is sealed it is a compiler error if you try to extend it.

    Hiding a method means that you redefine a non-virtual method in a derived class.  Bad practice nearly all of the time.

    You can never hide a method in a sealed class.

    Evan

    Thursday, June 16, 2011 5:35 PM
  • I am curious too - I can't think of any way to override a method in a sealed class.

    The best you can do is try to extend the functionality using your own extension method. For example:

    public sealed class MyClass
    {
        public void Test()
        {
            Console.WriteLine("Test");
        }
    }
    
    public static class MyClassExtender
    {
        public static void ExtendedTest(this MyClass instance)
        {
            Console.WriteLine("Starting to test...");
            instance.Test();
            Console.WriteLine("Test finished");
        }
    }
    
    public static void Main()
    {
        MyClass test = new MyClass();
        test.ExtendedTest();
    }
    

    This will print:

    Starting to test...
    Test
    Test finished

    It obviously doesn't directly override the sealed class, but you can add some more functionality this way at least.

    Thursday, June 16, 2011 2:29 PM
  • Was wondering. Cant you create an object of the sealed class and call its method?

    Ofcourse, you cannot extend the methods since the class is declared as sealed - but still, oyou can utilize the method if you create an object of the class.

    Friday, June 17, 2011 8:56 PM
  • A sealed class cannot be inherited. Please check the official knowledge base on this: http://msdn.microsoft.com/en-us/library/88c54tsw(v=vs.71).aspx.

    >> how to access sealed class public method in to othere classes.
    I am wondering what do you mean by saying "access", while there is a situation that the class is not sealed but one or more members of the class is/are sealed. For example, if a method F() in the class B, which overrides a virtual method in class A, is declared to be sealed, then whithin another class C, the method F() can not be overrided, if you try to compile, an CS0239 error will occur.
    You can find more detailed info in this knowledge base: http://msdn.microsoft.com/en-us/library/88c54tsw.aspx.
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, June 18, 2011 5:55 AM
    Moderator

All replies

  • If by access you mean just accessing it, you have that ability already.  If you mean overriding it, you can't.  The best you can do is to hide it.  Which do you mean?

    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...

    Thursday, June 16, 2011 1:27 PM
  • Hi, I'm not the OP but noted you said you could hide it. How would you do that ? Just curious. Thanks in advance (and sorry for hijacking the thread).
    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Thursday, June 16, 2011 1:36 PM
  • I am curious too - I can't think of any way to override a method in a sealed class.

    The best you can do is try to extend the functionality using your own extension method. For example:

    public sealed class MyClass
    {
        public void Test()
        {
            Console.WriteLine("Test");
        }
    }
    
    public static class MyClassExtender
    {
        public static void ExtendedTest(this MyClass instance)
        {
            Console.WriteLine("Starting to test...");
            instance.Test();
            Console.WriteLine("Test finished");
        }
    }
    
    public static void Main()
    {
        MyClass test = new MyClass();
        test.ExtendedTest();
    }
    

    This will print:

    Starting to test...
    Test
    Test finished

    It obviously doesn't directly override the sealed class, but you can add some more functionality this way at least.

    Thursday, June 16, 2011 2:29 PM
  • You can't.

    When a class is sealed it is a compiler error if you try to extend it.

    Hiding a method means that you redefine a non-virtual method in a derived class.  Bad practice nearly all of the time.

    You can never hide a method in a sealed class.

    Evan

    Thursday, June 16, 2011 5:35 PM
  • Was wondering. Cant you create an object of the sealed class and call its method?

    Ofcourse, you cannot extend the methods since the class is declared as sealed - but still, oyou can utilize the method if you create an object of the class.

    Friday, June 17, 2011 8:56 PM
  • Was wondering. Cant you create an object of the sealed class and call its method?

    Ofcourse, you cannot extend the methods since the class is declared as sealed - but still, oyou can utilize the method if you create an object of the class.

    Yes - this is typically how sealed (non-static) classes are intended to be used. The OP never clarified on exactly what he meant by "access" the method, so we just kind of ran with the definition "override".
    Friday, June 17, 2011 9:17 PM
  • A sealed class cannot be inherited. Please check the official knowledge base on this: http://msdn.microsoft.com/en-us/library/88c54tsw(v=vs.71).aspx.

    >> how to access sealed class public method in to othere classes.
    I am wondering what do you mean by saying "access", while there is a situation that the class is not sealed but one or more members of the class is/are sealed. For example, if a method F() in the class B, which overrides a virtual method in class A, is declared to be sealed, then whithin another class C, the method F() can not be overrided, if you try to compile, an CS0239 error will occur.
    You can find more detailed info in this knowledge base: http://msdn.microsoft.com/en-us/library/88c54tsw.aspx.
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, June 18, 2011 5:55 AM
    Moderator
  • Hi sureshkumar,

    Is any progress made with our friends' suggestions?
    I am looking forward to hearing from you. Thanks.
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 21, 2011 9:40 AM
    Moderator