ATHENA
Java Programming Essentials / Smart Reader
THE ATHENA LIBRARY

Java Programming Essentials

Miriam Okafor · textbook

Chapter 7 · Inheritance

Extending a parent class

With the extends keyword, a SavingsAccount inherits every accessible field and method of Account. It may add new behaviour, such as addInterest(), and it may override existing methods to specialise them.

Inheritance models an is-a relationship. A SavingsAccount is an Account, so it can be used anywhere an Account is expected. That substitutability is the bridge to the most powerful idea in this book.

Use inheritance carefully. Deep hierarchies become fragile; a change in a parent ripples through every descendant. Many Java designers prefer shallow hierarchies combined with interfaces.

Fictional educational sample written for the Athena demo · Page 131 of 412.

75%saved