Middle — Senior
How can the addition of new subclasses to an existing class be prevented?
sobes.tech AI
Answer from AI
In Java, the final keyword is used to prevent the creation of subclasses from an existing class. By declaring a class as final, you prohibit inheritance from it. For example:
public final class MyClass {
// class body
}
Attempting to create a subclass of MyClass will result in a compile-time error. This is useful when you want to ensure that the behavior of a class is not altered through inheritance.