• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 949
  • Last Modified:

Inheritance - private and sealed?

Hi
whats the difference between a private and sealed field / method?
Thanks
Paul
0
paulwhelan
Asked:
paulwhelan
1 Solution
 
SmithJWCommented:
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
0
 
paulwhelanAuthor 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
0
 
gdupadhyayCommented:
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
adatheladCommented:
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.
0
 
paulwhelanAuthor 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)
0
 
mrichmonCommented:
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.
0
 
dkloeckCommented:
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.

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now