Java Inheritance Part 2

The syntax for defining a subclass class that inherits from another class is to add an extends clause in the class header:

Screen Shot 2018-06-30 at 1.54.33 PM

The extends keyword specifies that the subclass inherits members of the superclass. That means that the subclass begins with a set of predefined methods and fields inherited from its hierarchy of superclasses.

JFrame allows us to create graphical applications. So we can use that to create our subclass of StatisticalDispersion and all of its inheriting subclasses.

Here, StatisticalDispersion is the subclass under JFrame. And it inherits from all the classes that JFrame inherits from, all the way back to the Object class.

Screen Shot 2018-06-30 at 3.24.57 PM

The StatisticalDispersion Class Hierarchy

We are coding a class named StatisticalDispersion that extends the JFrame class, so we use the following header:

Screen Shot 2018-06-30 at 3.44.19 PM

Because our StatisticalDispersion class extends JFrame, it inherits more than 300 methods and more than 30 fields. That’s because the JFrame class is a subclass of Frame, which is a subclass of Window, which is a subclass of Container, which is a subclass of Component, which is a subclass of Object.

A hierarchy is composed of subclasses which inherit methods and fields. Here, we make all of them available to the StatisticalDispersion class.

Subclasses do not necessarily need to use their inherited methods, but these are available if needed. The programmer does not need to write methods and define fields in classes which have already inherited them.

JFrame is the direct superclass of our StatisticalDispersion class. As you can see in the image, StatisticalDispersion refers to it. And as you can see in the code, JFrame follows the extends clause.

A class can have multiple direct subclasses but only one direct superclass. One can have many offspring but yet can only develop from one zygote.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s