Book Image

Java 9 Cookbook

By : Mohamed Sanaulla, Nick Samoylov
Book Image

Java 9 Cookbook

By: Mohamed Sanaulla, Nick Samoylov

Overview of this book

<p>Java is an object-oriented programming language. It is one of the most widely accepted languages because of its design and programming features, particularly in its promise that you can write a program once and run it anywhere.</p> <p>This cookbook offers a range of software development examples in simple and straightforward Java 9 code, providing step-by-step resources and time-saving methods to help you solve data problems efficiently. Starting with the installation of Java, each recipe addresses a specific problem, with a discussion that explains the solution and offers insight into how it works.</p> <p>We cover major concepts such as Project Jigsaw and various tools that will enable you to modularize your applications. You will learn new features in the form of recipes that will make your applications modular, secure, and fast.</p>
Table of Contents (22 chapters)
Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Using inheritance and composition to make the design extensible


In this recipe, you will learn about two important OOD concepts, namely Inheritance and Polymorphism, which have been mentioned already and used in the examples of the previous recipes.

Getting ready

Inheritance is the ability of one class to extend (and, optionally, override) the properties and/or methods of another class. The extended class is called the base class, superclass, or parent class. The new extension of the class is called a subclass or child class.

Note

Polymorphism is the ability to use the base class as a type for the references to the objects of its subclasses.

To demonstrate the power of these two concepts, let's create classes that represent cars and trucks, each having weight, engine power, and speed it can reach (as a function of time) with maximum load. In addition, a car, in this case, will be characterized by the number of passengers, while a truck's important feature will be its payload.

How to do it...

  1. Look...