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

What is an abstract class c#?

492


Can a static class have a constructor c#?

483


How do you specify a custom attribute for the entire assembly (rather than for a class)?

525


What is the extension of c# file?

440


What is console programming language?

487






What is the difference between func and action delegate?

491


Is there any sample c# code for simple threading?

567


Explain how to use an extender provider in the project.

490


What is a console operator?

495


How can we make a thread sleep for infinite period ?

546


What is var c#?

493


Is c++ or c# better?

544


What are regular expressions? Search a string using regular expressions?

518


Why abstract class is not instantiated in c#?

526


What are strings in c#?

493