CHAPTER 12

image

Inheritance

Inheritance allows a class to acquire the members of another class. In the example below, Apple inherits from Fruit. This is specified with the extends keyword. Fruit then becomes the superclass of Apple, which in turn becomes a subclass of Fruit. In addition to its own members, Apple gains all accessible members in Fruit, except for its constructors.

// Superclass (parent class)class Fruit{   public String flavor;}  // Subclass (child class)class Apple extends Fruit{   public String variety;}  

Object

A class in Java may only inherit from one superclass, and if no class is specified it will implicitly inherit from Object ...

Get Java Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.