Inheritance






Inheritance in the Real World


Mom and Daughter

Inheritance is an important concept to understand in Java.

Think about your parents and the features that they have - their eyes, hair, nose, etc. Which of those do you have? Why did you get them?

Let's consider genetics. It’s the process where genetic information is passed from parent to child. Java’s inheritance is very similar with genetic inheritance!

Just like genetics, in Java we have “parent” classes (or “Super” classes). These parent classes hold methods and fields that can be passed down to other classes. The methods and fields can be considered our mother's eyes and our mother could be considered the “Super” class.

In Java, when we declare inheritance on a class, we will be given the methods and fields from that class, just like taking the features from our parents.

Inheritance Syntax


Extends: used to inherit the properties of a class

Super:

Instanceof: used to test whether the object is an instance of th specified type (class, subclass or interface)

In Action


Inheritance Example