Results 1 to 6 of 6

Thread: Sealed Classes in C#

  1. #1
    Join Date
    Oct 2005
    Posts
    2,393

    Sealed Classes in C#

    Hi, Can anyone explain me about the Sealed Classes in C#? I don't know what is C# basically. But, I have to create a Case study on Sealed Classes in C#. I have enough information to do it, But it is not giving me any type of information which will tell me what exactly sealed class is. So, just give me clear and simple assistance of it.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Sealed Classes in C#

    Hi, Sealed keyword is used to prevent derivation from a class. That means you will get an error if you specify sealed class as the base class of another class. It wont be declared as abstract also. Basically, a sealed class is known to never have any derived classes.

    That is why you won't be able to derive anything from structs as they are sealed. Take a Example code as below:

    Code:
    using System;
    sealed class Testing
    {
       public int a; 
       public int b;
    }
    
    class Maintest 
    {
       public static void Main() 
       {
          Testing tt = new Testing(); 
          tt.a = 110;
          tt.b = 150;
          Console.WriteLine("a = {0}, b = {1}", tt.a, tt.b); 
       }
    }
    In the preceding example, if you attempt to inherit from the sealed class by using a statement like this:

    Code:
    class DerivedTesting: Testing {} // Error will be thrown
    You will get the error message such as :
    'DerivedTesting' cannot inherit from sealed class 'Testing'.

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Sealed Classes in C#

    Hello, Actually in C# a method can't be declared as sealed method. When you override a method in a derived class, at that time you can declare the overrided method as sealed. If you declared any method as sealed that means you are avoiding further overriding of that particular method.
    Code:
    using System;
    class sealing 
    {
       public int a; 
       public int b;
    
       public virtual void One()
       {
    	Console.WriteLine("Virtual method");
       }
    }
    
    class sealing : sealing1
    {
       public override sealed void One()
       {
    	Console.WriteLine("Sealed method");
       }	
    }
    
    class OneMain
    {
       public static void Main() 
       {
          sealing1 ss = new sealing(); 
          ss.a= 50;
          ss.b = 60;
          Console.WriteLine("a = {0}, b= {1}", ss.a, ss.b); 
          ss.One();
       }
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Sealed Classes in C#

    Hi, You want to know about Sealed Classes. It is very simple. A class which wont allow you to inherit the base class for security reason is know as sealed class. That's it. So, now you can say that, sealed class is the last class in hierarchy. It is possible that the class which is declared as sealed is a derived from the base class, but it is never a base class. Now, if you want to access the members of sealed class then you need to create the instance of the object. I think that it is developed for the security as no one can able to make the copy of software by overriding the class and getting the logic to be used.

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Sealed Classes in C#

    Hi, you want to create a case study about the sealed class and you told that you are having enough material to do that. So, you basically need the idea about the sealed class. So, Sealed classes are the classes which will not be inherited by any class or it can't be overridened. So, in simple words, sealed classes are those classes which can't be used for making inheritance.

  6. #6
    Join Date
    May 2008
    Posts
    2,012

    Re: Sealed Classes in C#

    Hi, A class which restricts inheritance for security region is called Sealed class. That means you can't create any class from inheriting the class which is declared as sealed. You are going to create a case study on sealed classes, How can you able to make, as sealed class is not that much big topic to make casestudy on it. How many pages are required in casestudy?

Similar Threads

  1. Benefits of sealed SIM on HTC EVO 4G LTE
    By Honeykay in forum Portable Devices
    Replies: 1
    Last Post: 22-05-2012, 12:45 PM
  2. Sealed Media Unsealer
    By Muwafaq in forum Operating Systems
    Replies: 5
    Last Post: 23-04-2010, 12:10 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,144,531.36834 seconds with 17 queries