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 private virtual in C#?
Why do we need singleton pattern in c#?
Explain how do I convert a string to an int in c#?
Is it possible to inherit multiple interfaces?
How to generate strong name key file or which command is used to generated strong name key file?
How do I start a program in c#?
How do I know if executenonquery is successful c#?
Explain About web methods and its various attributes
Why use “using” in c#?
What are variables in c#?
Is xml tags are case sensitive?
What is the use of flag in c#?
what are windows services?
Give an example of a directcast.
Is c# substring zero based?