Book Image

Java 9 with JShell

By : Gaston C. Hillar
Book Image

Java 9 with JShell

By: Gaston C. Hillar

Overview of this book

The release of Java 9 has brought many subtle and not-so-subtle changes to the way in which Java programmers approach their code. The most important ones are definitely the availability of a REPL, known as JShell, which will make experiments and prototyping much more straightforward than the old IDE-based project-led approach. Another, more subtle change can be seen in the module system, which will lead to more modularized, maintainable code. The techniques to take full advantage of object-oriented code, functional programming and the new modularity features in Java 9 form the main subjects of this book. Each chapter will add to the full picture of Java 9 programming starting out with classes and instances and ending with generics and modularity in Java.
Table of Contents (23 chapters)
Java 9 with JShell
Credits
About the Author
Acknowledgement
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Index

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);...