Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » Visual C# » 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 :
Page Views : 28951
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
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 | 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 | 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 | 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 | 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 | 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 | Modify 
Re: Nice Article.. by Puran On June 9, 2009
Thanks Gopinathbabu.
Reply | Email | 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 | 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 | 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 | Modify 
Nice Article by Maharajan On March 5, 2010
It is easy to understand. Good job. Keep it up.....
Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.