8.3 Polymorphism
What’s in a name? That which we call a rose
By any other name would smell as sweet.
—WILLIAM SHAKESPEARE, Romeo and Juliet
Inheritance allows you to define a base class and derive classes from the base class. Polymorphism allows you to make changes in the method definition for the derived classes and have those changes apply to the methods written in the base class. This all happens automatically in Java, but it is important to understand the process.
Dynamic Binding and Inheritance
Consider a program that uses the Person
, Student
, and Undergraduate
classes as depicted in Figure 8.4. Let’s say that we ...
Get Java: An Introduction to Problem Solving and Programming, 8th Edition 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.