Blue Theme Orange Theme Green Theme Red Theme
 
MindFusion's Components
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » C# Language » What are sealed classes and sealed methods

What are sealed classes and sealed methods

In this article, I will try to explain sealed classes and sealed methods in a very simple way in C# language.

Author Rank:
Technologies: .NET 1.0/1.1, .NET 2.0, .NET 3.0 and 3.5,Visual C# .NET
Total downloads :
Total page views :  4293
Rating :
 3.6/5
This article has been rated :  5 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
Become a Sponsor


Related EbooksTop Videos


What is sealed class and sealed method?

 

In this article I will try to explain sealed class and sealed method in a very simple way.

 

Sealed Class

 

Sealed class is used to define the inheritance level of a class.

 

The sealed modifier is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class.

 

Some points to remember:  

1.  A class, which restricts inheritance for security reason is declared, sealed class.
2.  Sealed class is the last class in the hierarchy.
3.  Sealed class can be a derived class but can't be a base class.
4.  A sealed class cannot also be an abstract class. Because abstract class has to provide functionality and here we are
     restricting it to inherit.

Practical demonstration of sealed class

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace sealed_class

{

    class Program

    {

        public sealed class BaseClass

        {

            public void Display()

        {

            Console.WriteLine("This is a sealed class which can;t be further inherited");

        }

    }

 

        public class Derived : BaseClass

        {

            // this Derived class can;t inherit BaseClass because it is sealed

        }

   

        static void Main(string[] args)

        {

            BaseClass obj = new BaseClass();

 

            obj.Display();

 

            Console.ReadLine();

        }

    }

}

 

Sealed Methods

 

Sealed method is used to define the overriding level of a virtual method.

 

Sealed keyword is always used with override keyword.

 

Practical demonstration of sealed method

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace sealed_method

{

    class Program

    {

        public class BaseClass

        {

           

            public virtual void Display()

            {

                Console.WriteLine("Virtual method");

            }

        }

 

       public class DerivedClass : BaseClass

        {

            // Now the display method have been sealed and can;t be overridden

            public override sealed void Display()

            {

                Console.WriteLine("Sealed method");

            }

        }

 

       //public class ThirdClass : DerivedClass

       //{

 

       //    public override void Display()

       //    {

       //        Console.WriteLine("Here we try again to override display method which is not possible and will give error");

       //    }

       //}

 

        static void Main(string[] args)

        {

 

            DerivedClass ob1 = new DerivedClass();

            ob1.Display();

 

            Console.ReadLine();

        }

    }

}

 

Hope this article will give you better view of sealed class and sealed method. Waiting! for your valuable feedback.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Puran Mehra

Working as a Software professional. 

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Dundas Dashboard
Become a Sponsor
 Comments
Good article by Mahesh On May 25, 2009
Keep up the good work Puran.
Just post C# language related articles in C# Language section only. Going good.
Reply | Email | Delete | Modify | 
Queried Related to this article by Sumaira On June 4, 2009
A simple and very nice article, i have two queries Mehra

What is the advantage to use sealed classes and in what kinda situation we should prefer to use sealed classes?

Second question is, u wrote in this article "Sealed class can be a derived class but can't be a base class.
What is the difference between a derived class and base class?

Thanks,
Reply | Email | Delete | Modify | 
Re: Queried Related to this article by Puran On June 5, 2009
Dear,

I have answered your query to your gmail account. I would appreciate if you give rating to my articles as they help in doing me good work.

Thanks,

Puran
Reply | Email | Delete | Modify | 
thanks for you response by Sumaira On June 5, 2009
Thanks for your very quick reply Mehra...
your reply helped me in resolving few confusions about sealed classes
:)
Reply | Email | Delete | Modify | 
Re: thanks for you response by Puran On June 7, 2009
Your are welcome Sumaira. In article rating 1 is the lowest and 5 is the highest.
Reply | Email | Delete | Modify | 
Nice Article.. by Gopinath On June 9, 2009
This Article..is Excellent...It is very simple & easy to understand...Thank u so much Mehra..for Providing such a good article.
Reply | Email | Delete | Modify | 
Re: Nice Article.. by Puran On June 9, 2009
Thanks Gopinathbabu.
Reply | Email | Delete | Modify | 
This questions was already posted and you replied through mail, Please Post the reply so others like me also know the answers by AnthonyBenedict On June 19, 2009
What is the advantage to use sealed classes and in what kinda situation we should prefer to use sealed classes?

Second question is, u wrote in this article "Sealed class can be a derived class but can't be a base class.
What is the difference between a derived class and base class?
Reply | Email | Delete | Modify | 
Re: This questions was already posted and you replied through mail, Please Post the reply so others like me also know the answers by Puran On June 19, 2009

Dear Anthony,

 

Here is your answer to the queries:

 

What is the advantage to use sealed classes and in what kind of situation we should prefer to use sealed classes?

 

Sealed class is used to define the inheritance level of a class. If you don’t want a class to be inherited further you declare the class as seal.

 

In a way you are stopping access to the class, which is sealed. Here the concept of OOPs comes. Exposing those features, which are required. Sealed class, restricts inheritance for security reasons.

 

Sealed class can be a derived class but can't be a base class.

 

Base class is the parent class of a derived class. Classes may be used to create other classes. A class that is used to create (or derive) another class is called the base class. The class, which is derived, is called derived class.

 

Sealed class can be a derived class but can't be a base class. Sealed class is the last class in the hierarchy. An error occurs if a sealed class is specified as the base class of another class.

Reply | Email | Delete | Modify | 
Re: Re: This questions was already posted and you replied through mail, Please Post the reply so others like me also know the answers by AnthonyBenedict On June 19, 2009
Thanks for your prompt reply
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved