How can you prevent classes to be inherited?
Answer Posted / asit nutiyal(birla)
sealed modifier is used for preventing the accident
inhritance of a class. A sealed class can not be inhrited.
Example :
using system;
sealed class abc
{
protected string str = "";
public virtual void hello()
{
str = " Hi i am not in C#";
}
class xyz : abc //--ERROR--
{
public override void hell0()
{
str = "hi i am in C#";
Console.WriteLine("{0}",str);
}
}
class mainclass
{
public static void Main()
{
xyz x = new xyz();
x.hello();
}
}
Note-> if we run above program thn we will get an error
because we inheit class abc while it is sealed.
| Is This Answer Correct ? | 4 Yes | 0 No |
Post New Answer View All Answers
What is object pool in .net?
Why data types are important in c#?
What is a dbml?
Are arrays immutable c#?
Explain the feature of c# language?
Will the following code compile and run?
How will you allow a class to be inherited, but prevent the method from being over-ridden?
How to find type of variable?
If you define a user defined data type by using the class keyword, is it a value type or reference type?
What is the symbol used for in c#?
Does c# support properties of array types?
Is string passed by reference in c#?
what is c# command?
What tool we have to use to install assembli in gac folder.
How to implement an object pool in c#.net.