Junior — Middle
Is it possible to declare a class as final in Java or a similar system to prevent inheritance?
sobes.tech AI
Answer from AI
Yes, in Java, you can declare a class as final to prevent it from being inherited. This is done using the final keyword before the class declaration:
public final class MyClass {
// class body
}
After this, no other class can inherit from MyClass. This is useful when you want to ensure that the behavior of the class is not altered through inheritance.
Similar mechanisms exist in other object-oriented programming languages, for example, in C# a sealed class is used.