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 interchangeably; both are acceptable.

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

{

void eat()

{

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

}

}


class Programmer extends Person

{

void eat()

{

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

{

void learn()

{

read();
practice();

application();

}

}


class Programmer extends Person

{

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.

RESOURCES

Coding Train - Inheritance Part 1 

Coding Train - Inheritance Part