Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent class 'a' from being inherited by another class?

I have a class named fdetails and I do not want any other class to inherit from this class.

Can I set it to not being inherited by another class. I would like to get this done in the following 3 languages:

  • Java
  • VB.NET 3.5
  • C# 3.5
like image 452
vishnu Avatar asked Nov 04 '10 11:11

vishnu


People also ask

Can you restrict a class from inheriting another class?

To prevent inheritance, use the keyword "final" when creating the class. The designers of the String class realized that it was not a candidate for inheritance and have prevented it from being extended.

How do you make a class not inheritable?

There are 2 ways to stop or prevent inheritance in Java programming. By using final keyword with a class or by using a private constructor in a class.

Is it possible to restrict inheritance?

You cannot restrict inheritance in javascript. If you have a public constructor function that initializes an object, any other object can use it to make a derived object.

How do you prevent a class from being inherited in C++?

Solution-1: Use the final keyword (from C++11) So, it'll not be inherited by any class. If you try to inherit it, the compiler will flash an error that “a final class cannot be used as a base class“. Note that you can create an object of the final class as show in the main() method here.


2 Answers

java: final  
vb: NotInheritable (NonOverrideable for properties)
c#: sealed
like image 54
Femaref Avatar answered Sep 29 '22 11:09

Femaref


In Java use the final keyword:

public final class fdetails{

}

In C# use the sealed keyword:

public sealed class fdetails{

}

In VB.net use the NotInheritable keyword:

public notinheritable class fdetails

end class
like image 42
chillysapien Avatar answered Sep 29 '22 13:09

chillysapien