Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 27-11-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
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.
Reply With Quote
  #2  
Old 27-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
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'.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #3  
Old 27-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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();
   }
}
Reply With Quote
  #4  
Old 27-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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.
Reply With Quote
  #5  
Old 27-11-2009
Member
 
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.
Reply With Quote
  #6  
Old 27-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
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?
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Sealed Classes in C#"
Thread Thread Starter Forum Replies Last Post
Benefits of sealed SIM on HTC EVO 4G LTE Honeykay Portable Devices 1 22-05-2012 12:45 PM
Sealed Media Unsealer Muwafaq Operating Systems 5 23-04-2010 12:10 AM


All times are GMT +5.5. The time now is 02:07 PM.