The Coder's Handbook   

Inheritance

WHAT IS INHERITANCE?

Problem of Similar Classes


When you write a program, you will often find that you have many classes that share code because they're similar to one another.  For example, in your Fireworks project - how often did you find yourself copy-pasting large chunks of code?


This is actually a big problem:


Inheritance


To solve this problem, we'll use inheritance.  This allows us to define one class - called a superclass - that shares all of its traits with a subclass.  


They will automatically "inherit" the classes objects and methods.  This means we won't need to define them in the subclass, and can even modify them!


Parent / Child


We also use the term parent class to refer to the superclass, and child class to refer to the subclass.  You'll hear those terms used interchangablely; both are acceptable.


In Java, every child has exactly one parent and parents can have an infinite number of children, but do not know that they exist.


Java families are weird.

AN EXAMPLE

The Person Superclass


Let's consider that you might define a person class.  Every object of class person has a set of traits that define it.


Every person has two piece of data and three methods:

The Programmer Subclass


To make a subclass, you'll use the keyword extends.  For example, we might write:


public class Programmer extends Person


This means that a programmer "is-a" person.  You can now create a Programmer object, and freely call any method defined in Person!


But a programmer isn't just a copy of its superclass.  It has an additional piece of data and method, which we'd write the same way you would write it in a normal class. 


This means that a programmer has all of the functionality of a Person, but only needs to add a small amount of code for its own special data and method!

OVERRIDING METHODS

Doing Things My Own Way


But wait, there's more!  What if a programmer does something that a normal person does, but in a different way?  You can override any method from the superclass, providing it new behavior.


class Person

{

public void eat()

{

System.out.println("Eats at a table like a normal human.");

}

}


class Programmer extends Person

{

public void eat()

{

System.out.println("Grabs snacks to fuel coding rampage.");

}

}


In this example, when you create a Programmer and call the eat() method, it will always use the behavior of the subclass.  


Remember: the method from the superclass is only called if you do not provide a more specific implementation in the subclass!

No Jimothy!  Do not ingest the computer.

USING SUPER

Like My Parent, But Different


Okay, but what if I mostly want to copy the behavior from my super class, but change it in a small way?  Like, I do everything the superclass does... but I add a little something else?


To do so, you'll use the super keyword to call the parent's version of the method.  


class Person

{

public void learn()

{

read();
practice();

application();

}

}


class Programmer extends Person

{

public void learn()

{

complain();

procrastinate();

super.learn();

}

}


In this example, a programmer will do all the steps needed to learn - but will also first complain and procrastinate.  This allows you to avoid repeating code, but still make the subclass unique.

ACCESS LEVELS

Public and Private


So far we've learned about two access levels


When we use inheritance, we'll often take advantage of a third access level.


Protected


When a method or piece of data is protected, it means that only this class or a subclass of this class can access it.


This is commonly used to store data that a subclass will need to modify, but you still want to keep private from the rest of the program.

RESOURCES

Bro Code - Inheritance

Alex Lee - Inheritance in Java

Alex Lee - Super Keyword

Coding Train - Inheritance Part 1  (Processing)

Coding Train - Inheritance Part 1  (Processing)

Coding With John - Why Java Creators Rejected Multiple Inheritance