We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Inheritance - private and sealed?

paulwhelan
paulwhelan asked
on
Medium Priority
964 Views
Last Modified: 2008-06-30
Hi
whats the difference between a private and sealed field / method?
Thanks
Paul
Comment
Watch Question

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

http://msdn2.microsoft.com/en-us/library/88c54tsw.aspx

Author

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

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

Thanks
Paul
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).

Example:
sealed class MyClass
{
  public string name = "Abhinaba";
}

class AnotherClass : MyClass                // Gives Compilation Error
{

}

I think u get clear picture abt that.

Thanks
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
"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)

Commented:
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.

Commented:
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.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.