locked
Sealed and Private class RRS feed

  • Question

  • User-1916576194 posted

    Hi,


    Can any one please let me know the difference between Sealed class and Private class.


    Thanks!!

    Monisha.

    Tuesday, June 1, 2010 12:29 PM

Answers

  • User-1237044210 posted

    Hi Monisha,        

    There is having some differences between private class and sealed class.

    + Private class usually used on inner class, that don't want to external to seeing it, it is created the closure. 

    For example,

    public class Parent {
           // some thing at here
           private readonly SubClass sc;
           
           // ctor
           public Parent () {
                  sc = new SubClass();
           }
           
           public string foo () {
                 return sc.bar();
           }
          
           private class SubClass {
                 // have some thing here
                public string bar() {
                     //..............
                     return "...........";
                }
           }
    }

    + Sealed class is meaning: you can't inherit from it, it is a finally class in inherited hierarchy (leaf node).

    For examples, in .NET 2.0, Microsoft built HttpContext is a sealed class, and in .NET 3.5  they are wrapper it became HttpContextWrapper for Unit Testing it.

    That is my knowledge about 2 of its. If it have any problems, you can post at here, and we shall discuss later.

    Regards,

    ThangChung


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 1, 2010 12:53 PM
  • User-1465070452 posted

    Sealed classes cannot be inherited

    http://msdn.microsoft.com/en-us/library/88c54tsw%28v=VS.80%29.aspx

    Classes declared Private cannot be used  direclty anywhere in the application and hence cannot be inherited...

    it gives complie time error

    you have to nest the private class inside a public class like below

    public class SuperPerson
        {
            private class Person
            {
            }
        }

    More on access modifiers should give you a better idea

    http://msdn.microsoft.com/en-us/library/ms173121%28v=VS.80%29.aspx



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 1, 2010 12:54 PM
  • User917890880 posted

    Private: Private limits the visiblity to a scope. Declaring a private class within a class means that "sub-class" can't be seen from outside of the class. This is also true for methods and properties - they can be seen within the class, but not to any consumers or inheritors. Private keyword is used for declaring class.


    Sealed: If a class is declared as sealed, that means that you cannot inherit from the class. Sealed class can be used when a class is internal to the operation of the library, class or whwn you donot want that class to be overridden because it may affect the functionality. Sealed keyword is used for declaring class 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 1, 2010 1:04 PM
  • User165950705 posted
    The Difference between sealed class and private class
    is not same bcoz in private class the user can call the function in
    same class and if the user has to call sealed class then the methods
    function can be called throught other class also


    when a class defined as sealed its not possible to inherit 
    its class and properties to the inherited class.Its also
    applicable to the class properties.Even class defined as
    public its banned to inherit the class.


    the diffenence between the TWO is that: 

    In private Class: We can create a constructor and therefore
    we can create an instance of that class.

    But
    In Sealed class we can not create a constructor of that
    class, so no instance of that class is possible.


    Private Constructor of a Private Class = Sealed Class.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 1, 2010 1:23 PM

All replies

  • User-1237044210 posted

    Hi Monisha,        

    There is having some differences between private class and sealed class.

    + Private class usually used on inner class, that don't want to external to seeing it, it is created the closure. 

    For example,

    public class Parent {
           // some thing at here
           private readonly SubClass sc;
           
           // ctor
           public Parent () {
                  sc = new SubClass();
           }
           
           public string foo () {
                 return sc.bar();
           }
          
           private class SubClass {
                 // have some thing here
                public string bar() {
                     //..............
                     return "...........";
                }
           }
    }

    + Sealed class is meaning: you can't inherit from it, it is a finally class in inherited hierarchy (leaf node).

    For examples, in .NET 2.0, Microsoft built HttpContext is a sealed class, and in .NET 3.5  they are wrapper it became HttpContextWrapper for Unit Testing it.

    That is my knowledge about 2 of its. If it have any problems, you can post at here, and we shall discuss later.

    Regards,

    ThangChung


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 1, 2010 12:53 PM
  • User-1465070452 posted

    Sealed classes cannot be inherited

    http://msdn.microsoft.com/en-us/library/88c54tsw%28v=VS.80%29.aspx

    Classes declared Private cannot be used  direclty anywhere in the application and hence cannot be inherited...

    it gives complie time error

    you have to nest the private class inside a public class like below

    public class SuperPerson
        {
            private class Person
            {
            }
        }

    More on access modifiers should give you a better idea

    http://msdn.microsoft.com/en-us/library/ms173121%28v=VS.80%29.aspx



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 1, 2010 12:54 PM
  • User917890880 posted

    Private: Private limits the visiblity to a scope. Declaring a private class within a class means that "sub-class" can't be seen from outside of the class. This is also true for methods and properties - they can be seen within the class, but not to any consumers or inheritors. Private keyword is used for declaring class.


    Sealed: If a class is declared as sealed, that means that you cannot inherit from the class. Sealed class can be used when a class is internal to the operation of the library, class or whwn you donot want that class to be overridden because it may affect the functionality. Sealed keyword is used for declaring class 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 1, 2010 1:04 PM
  • User165950705 posted
    The Difference between sealed class and private class
    is not same bcoz in private class the user can call the function in
    same class and if the user has to call sealed class then the methods
    function can be called throught other class also


    when a class defined as sealed its not possible to inherit 
    its class and properties to the inherited class.Its also
    applicable to the class properties.Even class defined as
    public its banned to inherit the class.


    the diffenence between the TWO is that: 

    In private Class: We can create a constructor and therefore
    we can create an instance of that class.

    But
    In Sealed class we can not create a constructor of that
    class, so no instance of that class is possible.


    Private Constructor of a Private Class = Sealed Class.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 1, 2010 1:23 PM