Chapter 11
Using Subclasses and Inheritance
IN THIS CHAPTER
Explaining inheritance
Creating subclasses
Using protected access
Creating final classes
AJava class can be based on another class. Then the class becomes like a child to the parent class: It inherits all the characteristics of the parent class, good and bad. All the visible fields and methods of the parent class are passed on to the child class. The child class can use these fields or methods as they are, or it can override them to provide its own versions. In addition, the child class can add fields or methods of its own.
In this chapter, you discover how this magic works, along with the basics of creating and using Java classes that inherit other classes. You also find out a few fancy tricks that help you get the most out of inheritance.
Introducing Inheritance
The word inheritance conjures up several noncomputer meanings:
- Children inherit certain characteristics from the parents.
- Children can also inherit behavior from their parents. As they say, the apple doesn’t fall far from the tree.
- When someone dies, their ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access