Skip to content
loading...

re: Why it is not possible to extend a final class in Java? VIEW POST

FULL DISCUSSION
 

The final keyword in Java means: „can‘t be modified“. Applied to a field/variable it means: it‘s a constant (the value can’t be modified.) Applied to a class, it means: class can’t be extended (and thus, modified).

 

Thanks, @bertilmuth .

As you said final keyword in java means "can‘t be modified", but still we should be allowed to extend it from another class so that we can use its defined method.

I totally agree that if we try to modify any of its methods then it should give an error, but otherwise we should be allowed to extend that final class and reuse its method in another class.

Please correct me if I am wrong anywhere.

 

As Kasey Speakman has pointed out, you can extend final classes by composition. Just not inheritance. A class can offer all visible methods of the final class, plus further methods.

 

Applied to a field/variable it means: it‘s a constant (the value can’t be modified.)

To be more correct: the variable assignment cannot be modified. Only in case of primitives and immutable objects the value cannot be modified.

final String[] foo = String[] { "bar" };
foo[0] = "quux"; // this is legal.
code of conduct - report abuse