Final
layout: default
title: final Classe
We can use the keyword final on a class so that other classes will not be able to extend it.
public final class FinalClass {
//...
}
public class FinalSubclass extends FinalClass { // ERROR, does not compile
//...
}
Practice Exercise¶
The visibility modifier and
finalcan also be in the orderfinal public. This compiles successfully.final public class FinalPublicClass { //... }
Drill¶
Inheritance/com.example.inheritance.drills* Create a classTestClass. It does not need amainmethod. * Try to makeTestClassextendSystemby addingextends System. * Try to makeTestClassextendString. * Try to makeTestClassextendStringBuilder. * Try to makeTestClassextendInteger. * It didn't work. MakeTestClassextendObject.