Inheritance and Interfaces

Lesson 13 of 20

Extend a class, implement an interface, and let one object wear two hats while the call stack climbs the family tree.

The big picture

Two kinds of promise

extends copies a parent class into your child; implements makes the child promise to deliver methods an interface demands.

extends = is-a

class Dog extends Animal — a Dog IS an Animal and inherits its fields and methods.

implements = can-do

An interface is a list of method promises. A class that implements it must write every one of them.

super calls up

super(...) runs the parent constructor first, so the inherited part of the object is ready.

One parent only

Java allows one superclass, but a class can implement many interfaces at once.