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

Posted on 2006-05-04
Last Modified: 2010-05-18
Can you allow class to be inherited, but prevent the method from being over-ridden
Question by:chuang4630
    LVL 52

    Accepted Solution

    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.
    LVL 2

    Expert Comment

    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() { }  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    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 video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now