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:
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.
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.
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.
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.
java: final
vb: NotInheritable (NonOverrideable for properties)
c#: sealed
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With