Inheritance - private and sealed?

Posted on 2006-03-24
Last Modified: 2008-06-30
whats the difference between a private and sealed field / method?
Question by:paulwhelan
    LVL 4

    Expert Comment

    A sealed field / method cannot be inherited from, or overridden.
    A private field / method can be overridden in an inherited class.

    Author Comment

    Would you be able to give an example that fits in with my inheritance example

    Classes =
    Mammal -> Human, Fish, Bird
    Bird -> NonFlyingBird, FlyingBird

    LVL 9

    Expert Comment

    Private Fields / Method :
    1. Declare with using private keyword.
    2. They can be overridden in derive class.
    3. It may be inherited.

    Sealed Fields / Method :
    1. Declare with using sealed keyword.
    2. A sealed method overrides a method of a base class, but it will not overridden in any derived class. So using sealed keywords with abstract gives error. A sealed class is sort of the opposite of abstract class. Bcoz it can be instantiated but cannot serve as a base class.
    3. A sealed class cannot be inherited from.
    4. Sealed is must to implement the Singleton (Design Pattern).

    sealed class MyClass
      public string name = "Abhinaba";

    class AnotherClass : MyClass                // Gives Compilation Error


    I think u get clear picture abt that.

    LVL 23

    Accepted Solution

    Suppose Mammal has a method declared as sealed. The Human, Fish and Bird classes that inherit the Mammal class, will not be able to override the method to change/add functionality. (i.e. the functionality provided by that method is the same across all derived classes and cannot be altered)

    If it wasn't declared as sealed, then they could override that method and provide their own interpretations.

    Author Comment

    "If it wasn't declared as sealed, then they could override that method and provide their own interpretations."

    I thought only 'virtual' methods could be 'override'ed (for want of a better word)
    LVL 35

    Expert Comment

    You also need to throw the "new" keyword into this discussion.

    You can override anything that is virtual or abstract - it hides the base implementation.

    Now if not decalred sealed you can use new to hide the base implemenatition of anything.
    LVL 10

    Expert Comment

    The sealed modifier can be applied to classes, instance methods and properties. A sealed class cannot be inherited. A sealed method overrides a method in a base class, but itself cannot be overridden further in any derived class. When applied to a method or property, the sealed modifier must always be used with override (C# Reference).

    Use the sealed modifier in a class declaration to prevent inheritance of the class, as in this example:

    sealed class SealedClass
        public int x;
        public int y;

    It is an error to use a sealed class as a base class or to use the abstract modifier with a sealed class.

    Structs are implicitly sealed; therefore, they cannot be inherited.


    The private keyword is a member access modifier. Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared, as in this example:

    class Employee
        private int i;
        double d;   // private access by default

    Nested types in the same body can also access those private members.

    It is a compile-time error to reference a private member outside the class or the struct in which it is declared.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 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

    13 Experts available now in Live!

    Get 1:1 Help Now