Click here to Skip to main content
15,912,932 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can anybody tell me about the Difference b/w private class vs sealed class with example.
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

Pleae refer:
diff between sealed class and private class[^]
difference between Sealed Class and Private Class[^]
 
Share this answer
 
Comments
Mohamed Mitwalli 30-May-12 2:43am    
5+
Prasad_Kulkarni 5-Jun-12 4:53am    
Thank you Mohamed!
i give you link i hope it will help you

C# Tweaks – Why to use the sealed keyword on classes?[^]

public abstract sealed class MyFundooClass {}[^]

Object-Oriented Programming in C# .NET - Part 4[^]

Private : Only members of class have access. but it can be inherit in any class.
If you define a class as “Sealed” in C# and “NotInheritable” in VB.NET you can not
inherit the class any further.

now i will more clear with simple example

C#
private class abc // private class it
   {
       void abc()
       {
       }
       public  void show()
       {
           string abc = "";
       }
   }

   sealed class efg // Sealed class it
   {
       void efg()
       {
       }
       public void showefg()
       {
           string abc = "";
       }
   }

   public class cde : abc // in this class we inherit private class abc
   {
       void cde()
       {
           show(); // private class method show
       }
   }

   public class ghi : efg // we cannot inherit sealed class and it method
   {
       void ghi()
       {
           // not showing sealed class method  showefg() even it is public
       }
   }


protected void Page_Load(object sender, EventArgs e)
    {
        cde c = new cde();// calling private class method by inheritance
        c.show();
   }
 
Share this answer
 
v4
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.
C#
public class A
  {
  private class B
     {
     }
  B b = new B();
  }
public class C
  {
  A.B b = new A.B(); // ERROR
  }


C#
public sealed class A
   {
   }
public class B : A //ERROR
   {
   }
 
Share this answer
 
Comments
Mohamed Mitwalli 30-May-12 2:44am    
5+
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900