Inheritance - private and sealed?

Hi
whats the difference between a private and sealed field / method?
Thanks
Paul
paulwhelanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.