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


Please Help Members By Posting Answers For Below Questions

Is datetime immutable c#?

496


What is assembly and dll in c#?

460


Can properties be private in c#?

474


Explain the different types of delegates used in c#.

507


What does readonly mean in c#?

537






Is c sharp open source?

485


What is difference between array and collection?

466


Is c# good for beginners?

471


What is the difference between array and arraylist in c#?

480


What is platform independence"?

537


What is default parameter in c#?

504


What is the execution entry point for a c# console application?

526


How many bytes is an int in c#?

502


What does ienumerable mean?

478


What is the use of tryparse in c#?

462