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 do you mean by saying a "class is a reference type"?
What is default value of enum c#?
Do void methods have parameters?
What is event delegate in c#?
Contrast between an interface and abstract class?
Why we use get and set property in c#?
What operators can be used to cast from one reference type to another without the risk of throwing an exception?
Explain namespaces in c#.
What are managed providers?
Why do you call it a process? What’s different between process and application in .net, not common computer usage, terminology?
Is predicate a functional interface?
What is .net c#?
Why are c# strings immutable?
How to Show Message box in Metro Style App?
What is the difference between asp net and c#?