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
How does it work?
Can we store different data types in arraylist in c#?
What happens if the inherited interfaces have conflicting method names?
if a base class has a number of overloaded constructors, and an inheriting class has a number of overloaded constructors; can you enforce a call from an inherited constructor to a specific base constructor?
What is interpolation in programming?
Does main have to be static c#?
How do I download a program to my desktop?
Do void methods have parameters?
Is python easier than c#?
What is datetime minvalue in c#?
Illustrate the differences between the system.array.copyto() and system.array.clone()?
Why do we need oops in c#?
What is asp net c# corner?
What does console readkey do in c#?
Does c# support c type macros?