CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019
Lecture 6: Introduction to Inheritance
Objectives:
At the end of this lecture, students will be able to:
• define the term inheritance
• understand how inheritance works
• create a superclass, subclasses, override methods and extend classes
• understand polymorphism and the benefits
• use the class Object's methods and the class Class to get class type of objects.
• Use the @Override annotation and override the toString method
• understand how to use and override the equals method of the Object class.
• Understand how to cast objects.
• Work with abstract keyword.
Notes:
Inheritance allows you to create a class that's based on another class. When used correctly, inheritance
can simplify the overall design of an application. The following notes on the basic concepts of
inheritance.
How to work with inheritance
The figure below illustrates how inheritance works. When inheritance is used, a subclass inherits the
fields, constructors and methods of a superclass. Then the objects that are created from the subclass
can use these inherited members. The subclass can also provide its own members that extend the
superclass, and it can override methods of the superclass by providing replacement definitions for
them.
superclass Public fields
and methods
Private fields
subclass
Code that uses
inherited fields and
methods
New methods
Page 1 of 37
, CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019
Description
• Inheritance lets you create a new class based on an existing class. Then, the new class inherits
the fields, constructors and methods of the exisiting class.
• A class that inherits from an existing class is called a derived class, child class or subclass. A
class that another class inherits is called a base class, parent class or superclass.
• A subclass can extend the superclass by adding new fields, constructors and methods to the
superclass. It can also override a method from the superclass with its own version of the
method.
• To create a new window (called a frame in Java), you can code a class that inherits the Jframe
class that's in the javax.swing. Package. Then, your frame will inherit all the fields and methods
that are available to this class. Once you inherit the Jframe class, you can write code that uses
the inherited fields and methods, you can add controls to the frame and you can extend the
frame by coding new fields and methods.
The two classes shown in this figure above illustrates how this works. Here, the superclass is
javax.swing.JFrame. That's the Java API class that you can use to create a GUI window, which is called
a frame. As this figure shows, this class has several public fields and methods such as the
HIDE_ON_CLOSE field and the setTitle() method. This class has many more methods than the ones
shown here, though. This figure only shows a few representative ones. If you want to see all of them, in
NetBeans, when you import the javax.swing.JFrame class, put your cursor at the JFrame class name
and click Navigate menu, then on inspect choose members, you will see all of them there.
The subclass in this figure is the wsu.intra.cs.Calculator class and the diagram lists three groups of
members of this class. The first group shows the private members of this class, the second group shows
the code that uses the fields and methods that are inherited from the superclass. The group sets some
basic attributes of the frame, such as the title, location, and size. The third group includes two new
methods that have been added to the subclass.
How the Java API uses inheritance
The figure below shows a portion of the inheritance hierarchy that you can use to create a graphical
user interface that uses windows, buttons, labels, text boxes, combo boxes, check boxes and so on. This
illustrates how extensively inheritance is used throughout the Java API.
To start, all classes in Java inherit the Object class which is in the java.lang package. The java.lang
package is a package which is automatically imported in all java programs without you specifying the
import statement for it. Then, the Java API uses the classes in the Abstract Window Toolkit (AWT) to
define the classes for various GUI components. These classes are stored in the java.awt package.
However, they are an older technology that was primarily used with versions 1.0 and 1.1 of Java.
Since version 1.2 of Java, a GUI technology known as Swing has been available. These classes are
stored in the javax.swing package so they can use some of the code from these classes while improving
and extending this code.
For example, the Component class in the java.awt package provides features that are common to all
frames and controls. This class provides methods such as setLocation and setSize that set the location
Page 2 of 37
, CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019
and size of the component. Because these methods are provided by the Component class, they are
availble to all awt and swing components. That includes frames and the components that you place on
frames such as buttons, labels, text boxes and so on.
When you work with a class that inherits other classes, it's important to know that it can use fields and
methods from any of the classes in its inheritance hierarchy. For example, the JFrame class can use
fields and methods from the Frame, Window, Container, Component and Object classes.
The shaded classes in this figure are the GUI components that you will learn about in this course. All of
these classes are derived indirectly from the Component class. However, the JComponent class is the
direct parent of the swing controls for buttons, text boxes, labels and so on. Similarly, the Frame class
is the direct parent of the swing frame.
java.lang java.awt javax.swing
Object
Component
Container
JComponent
Swing classses for
buttons, lables,
Window
text boxes, etc
Frame JFrame
Description
• The Java API uses inheritance extensively in its own classes, so you often need to know what
the inheritance hierarchy is as you use these classes. For example, you need to know that all
classes ultimately inherit the Object class in the java.lang package.
• All Swing classes, which are stored in the javax.swing package, inherit the Component and
Container classes in the java.awt package. This package contains the Abstract Window Toolkit
(AWT) classes.
• A class can use the fields and methods of any of its super classes. For example, the JFrame
class can use the fields and methods provided by the Frame, Window, Container, Component
and Object classes.
Page 3 of 37
, CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019
How the Object class works
The figure below summaries the methods of the java.lang.Object class. Since every class
automatically inherits these methods, they are available from every object. However, since sub-classes
often override these methods, these methods may work slightly differently from class to class.
Perhaps the most used method of the Object class is the toString method. That's because the Java
compiler implicitly calls this method when it needs a string representation of an object. For example,
when you supply an object as the argument of the println method, this method implicitly calls the
toString method of the object.
When you code a class, you typically override the toString method of the Object class to provide more
detailed information about the object. Otherwise, the toString method will return the name of the
class and the hash code of the object, which is a hexadecimal number that indicated the object's
location in memory.
Unlike C++ and other languages that require you to manage memory, Java uses a mechanism known as
the garbage collector to automatically manage memory. When the garbage collector determines
that the system is running low on memory and that the system is idle, it frees the memory for any
objects that don't have any more references to them. Before it does that, though, it calls the finalize
method for each of those objects.
The Object class
java.lang.Object
Methods of the Object class.
Method Description
toString() Returns a String object containing the class name, followed by the @
symbol, followed by the hash code for this object. If that's not what you
want, you can override this method.
equals(Object) Returns true (boolean) if this object points to the same location in memory
as the specified object. Otherwise, it returns false, even if both objects
contain the same data. If thats not what you want, you can override the
equals method.
getClass() Returns a Class object that represents the type of this object.
clone() Returns a copy of this object as an Object object. Before you can use this
method, you must implement the Cloneable interface
hashCode() Returns the hashcode (int) for this object
finalize() Called by the garbage collector when the garbage collector determines
that there are no more references to the object
Page 4 of 37