Controlling subclassing of classes
The final
keyword has one additional usage. We can use final
as a modifier before the class
keyword in the class declaration to indicate Java that we want to generate a
final class, that is, a class that cannot be extended or subclassed. Java 9 won't allow us to create a subclass for a final class.
Now, we will create the VirtualDomesticCat
abstract class and then we will declare a concrete subclass named MaineCoon
as a final class. This way, we will make sure that nobody will be able to create a subclass of MaineCoon
. The following lines show the code for the VirtualDomesticCat
abstract class. The code file for the sample is included in the java_9_oop_chapter_07_01
folder, in the example07_02.java
file.
public abstract class VirtualDomesticCat extends VirtualDomesticMammal { public VirtualDomesticCat( int age, boolean isPregnant, String name, String favoriteToy) { super(age, isPregnant, name, favoriteToy);...