0

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?

Naresh Ravlani
  • 1,600
  • 13
  • 28
  • 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. – Peter Duniho Jun 25 '17 at 04:47
  • but nested class can be pvt ? @PeterDuniho –  Jun 25 '17 at 05:00
  • _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_. – Peter Duniho Jun 25 '17 at 05:01
  • @PeterDuniho so answer given by Rohit is wrong as he has made the 1st class abc as pvt which is wrong ? –  Jun 25 '17 at 05:04
  • 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). – Peter Duniho Jun 25 '17 at 05:06

3 Answers3

4

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 "...........";
            }
       }
}
Rohit Poudel
  • 1,793
  • 2
  • 20
  • 24
  • 1
    _"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. – Peter Duniho Jun 25 '17 at 04:51
  • 1
    @rohit how are u making 1st class a s pvt ? –  Jun 25 '17 at 05:07
  • 1
    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 – Rohit Poudel Jun 25 '17 at 05:10
  • but only nested class can be pvt !! @RohitPoudel –  Jun 25 '17 at 05:12
  • I have changed the example @Deepak Jain – Rohit Poudel Jun 25 '17 at 05:17
1

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.

Chetan
  • 6,711
  • 3
  • 22
  • 32
0

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();
        }
    }
}
bunbun
  • 2,595
  • 3
  • 34
  • 52
bvn
  • 1
  • 1