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 difference between static and readonly in c#?
Is c# slower than java?
Why do I get an error (cs1006) when trying to declare a method without specifying a return type?
Is int an object in c#?
What is an int c#?
what is collections in .net? why we use?
Why do we use constructors in c#?
how to insert the data from the grid view to database table though button click.pls send the answer to mail id suri1319@gmail.com
What are delegate methods?
Can we inherit static class in c#?
When do we generally use destructors to release resources?
How main method is called in c#?
How do you declare a method in c#?
How do you set a class path?
What is an object pool in .net?