What is the difference between a sealed class and a private class?

sealed class c#
private sealed class c
difference between sealed class and static class in c#
private class in c#
difference between private class and static class in c#
sealed class in java
difference between private constructor and sealed class in c#
can we inherit private class in c#

I know that you can not inherit from a class once sealed is used but I am confused what is the difference between these two: private and sealed?

Can't we make the base class members private if we don't want to inherit them instead of the whole class? What is the point of using sealed class?

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 why you do not want that class to be overridden because it may affect the functionality. sealed keyword is used for declaring class

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 "...........";
            }
       }
}

What is the difference between a sealed class and a private class , Now, you may think, then what is the difference between sealed and private because both the classes cannot be inherited, let us see the  Sealed has no consequences on the accessibility of a class. It just forbids inheritance. Also you need to remember that a top class, can only be public or internal. Therefore private is for sub classes only.

You need to understand difference between inheritability and accessibility.

If you want to make your class non-inheritable, making it sealed is the best option. Also a class can not be protected, private or internal protected. Only sub class can have those access specifiers. A normal class which is directly under a namespace can only be public or internal.

Now coming to you point of making all the members private in the base class. Doing that does not serve any purpose.

You inherit a class only to reuse certain properties and/or method or override them in the inherited class. If you make all the members private in the base class you won't be able access them outside even using the object of base class. Then what's the point of having them in the base class.

public class MyClass
{
    private void MyMethod() //You can not inherit this method but you can not use it using 'MyClass' also.
    {
        //Some code.
    }
}

MyClass myObj = new MyClass();
myObj.MyMethod(); // You can not do this as the method is private.

Now if you inherit this class in another class

public ChildClass : MyClass
{
    public void ChildMethod()
    {
        // Some Logic
    }
}

Now when you do

MyClass obj = new ChildClass();

You can not do

obj.MyMethod(); //coz this is private method.

You can not do following too.

obj.ChildMethod(); //coz that method is not known to MyClass.

So if you are making members private just for the sake of making them not available for inheritance, you are losing their accessibility from the base class too.

Getting Started With Sealed Classes in C#, C# .NET - difference between private class nad sealed class. Asked By R132 G132 on 23-Sep-10 08:21 AM. what is the difference between private class nad  Wats the diff between sealed class and private class in inheritance.. Answer / vishal. Sealed Class: When applied to a class, the sealed modifier prevents other. classes from inheriting from it. In the following example, class B inherits from class A, but no class can inherit. from class B.

Understood your confusion,

  1. First of all there is no independent private class inside a namespace, compiler throws an error.

  2. If you make a method void m1() private inside public class A, then the method m1 is not accessible from public class B.

  3. Sealed classes are accessible to other classes though it stops inheritance, meaning you cannot use it to derive from.

In the example below, you wont be able to access the method privatemethod from the Main(), but sealed class and sealed method can be accessed. So sealed can be accessed though cannot be inherited, that's the difference.

namespace ConsoleApp1
{
    using System;

    public class A
    {
        public virtual void test()
        {
            Console.WriteLine("Class A");
        }
    }

    public class C
    {
        public void testSealFromOutsideClass()
        {
            B instanceB = new B();
            instanceB.test();
        }
    }

    public sealed class B : A
    {
        public override void test()
        {
            Console.WriteLine("Class B");
        }


        private void Privatemethod()
            {

            Console.WriteLine("Printing from a private method");

            }
    }

    //public class C : B {}

    public class TestSealedClass
    {
        public static void Main()
        {
            A a = new B();
            a.test();

            C c = new C();
             c.testSealFromOutsideClass();
            B b = new B();

            Console.Read();
        }
    }
}

difference between private class nad sealed class, If you are pertaining to their usage for methods: sealed modifier prevents a derived class from overriding the method. While private modifier  A Private class can only be accessed by the class it is defined and contain within - it is completely inaccessible to outside classes. A Sealed class can be accessed by any class, but can not be derived from.

What is the difference between sealed and private?, Hi, Can any one please let me know the difference between Sealed class and Private class. Thanks!! Monisha. A sealed class doesn't allow inheritance. That is, you can't derive from it. A private constructor doesn't allow instancing (you can instance a sealed class). Also when you get deeper into the language, private constructors can be used in scenarios where you're overloading constructors.

Sealed and Private class, Hi, Private and Sealed classes having same significance or any difference is there ? Can you please explain? The private method is not inherited whereas sealed method is inherited but cannot be overridden. So, a private method cannot be called from sub-classes whereas sealed method can be called from sub-classes. The same private method can be defined in sub-class and it does not lead to error.

Main advantage of Private class and Sealed Classes , However, a method can be sealed in the classes in which they have been inherited. If you want to declare a method as sealed, then it has to be declared as virtual  Private: Sealed: Private classes cannot be declared directly inside the namespace. Sealed classes can be declared directly inside the namespace. We cannot create an instance of a private class. We can create the instance of sealed class. Private Class members are only accessible within a declared class.

Comments
  • Your question shows no evidence of research. Did you look at the documentation? Do you know what the sealed keyword does? Do you know what the private keyword does? Do you understand that most classes, not being nested within any other class, cannot be declared as private in the first place? Also, your second paragraph refers to private members, not private classes. Which is it you're asking about? It is not at all clear what you're even asking.
  • but nested class can be pvt ? @PeterDuniho
  • Only a nested class can be private. A non-nested class cannot be, because in that context, the accessibility private would deny access of the class to everyone.
  • @PeterDuniho so answer given by Rohit is wrong as he has made the 1st class abc as pvt which is wrong ?
  • The text in his answer suggests he understands that only nested classes can be private. Note the otherwise superfluous Page_Load() method at the end of his code example, which would make sense only inside a class declaration. So you might assume that the code he posted is an incomplete class declaration. Under that assumption, his answer isn't wrong, just very poorly written (i.e. confusing).
  • "sub-class" is a term used to describe a class that inherits some other class. Do not use it to describe nested classes. There is no Private keyword in C#; the language is case-sensitive, there is only private. Likewise, Sealed vs. sealed. You also have failed to answer the question; all of the information above is readily available in the documentation. You need to explain to the OP what the difference is, since that's what they asked. They could've read the documentation themselves, so clearly just paraphrasing the documentation isn't useful.
  • @rohit how are u making 1st class a s pvt ?
  • I dont understand what you want to know or want to asking Please say clearly.I am ready to explain..In C#, you can make class private by Using the keyword private. @Deepak Jain
  • but only nested class can be pvt !! @RohitPoudel
  • I have changed the example @Deepak Jain