Which one of the following class definitions is a valid definition of a class that cannot be extended?
-
abstract class Link { }
-
native class Link { }
-
final class Link { }
-
abstract final class Link { }
In Java, the final modifier prevents a class from being subclassed (extended). The abstract modifier requires a class to be extended, making abstract final an illegal combination. native is not a valid modifier for classes.
To answer this question, you need to understand the concept of class modifiers in Java.
Option A) abstract class Link { } - This option is incorrect because an abstract class can be extended by other classes.
Option B) native class Link { } - This option is incorrect because the "native" keyword is used to indicate that a method is implemented in a language other than Java, and it is not used to define a class that cannot be extended.
Option C) final class Link { } - This option is correct because the "final" keyword is used to indicate that a class cannot be extended. Therefore, this class cannot have any subclasses.
Option D) abstract final class Link { } - This option is incorrect because the "abstract" and "final" keywords are contradictory. An abstract class is meant to be extended, while a final class cannot be extended. It is not possible to have a class that is both abstract and final.
The correct answer is C) final class Link { }. This option is correct because the "final" keyword ensures that the class cannot be extended.