Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Can you allow class to be inherited, but prevent the method from being over-ridden

Posted on 2006-05-04
2
Medium Priority
?
247 Views
Last Modified: 2010-05-18
Can you allow class to be inherited, but prevent the method from being over-ridden
0
Comment
Question by:chuang4630
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 16609210
As long as the method is not virtual, or itself an override, then it cannot be overridden by a derived class. It can, however, be hidden by the derived class.
0
 
LVL 2

Expert Comment

by:shy_talk
ID: 16609457
Yes, you prevent inheritance, either by not using the "virtual" keyword when you declare the method in the base class, or by using the "sealed" keyword if you are inheriting a method [that is already virtual in the base class]. Drop this code into a new console app to illustrate. Should be OK.

   class BaseClass
   {
      // Overrideable
      protected virtual void VirtualMethod() { }
      protected virtual void VirtualMethodThatWillBeSealed() { }
     
      protected void NonVirtualMethod() { }
   }
   
   class Subclass : BaseClass
   {
      // Overrides OK
      protected override void VirtualMethod() { }

      // Can override and seal the other virtual method
      protected sealed override void VirtualMethodThatWillBeSealed() { }
     
      // Cannot override the non-virtual method!!!      
      protected override void NonVirtualMethod() { }
   }
   
   
   class SubSubclass : Subclass
   {
      // Virtual method that was not sealed still overrides OK
      protected override void VirtualMethod(){}

      // But this method was sealed, do does not compile!!!
      protected override void VirtualMethodThatWillBeSealed() { }  
   }
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question