Tuesday, February 5, 2013

Inheritance

What Is Inheritance?
As the name inheritance suggests an object is able to inherit characteristics from another object. In more concrete terms, an object is able to pass on its state and behaviors to its children. For inheritance to work the objects need to have characteristics in common with each other.
For example, let's say we make a class called "Human" that represents our physical characteristics. It's a generic class that could represent you, me or anyone in the world. Its state keeps track of things like number of legs, number of arms, and blood type. It has behaviors like eat, sleep, and walk. Human is good for getting an overall sense of what makes us all the same but it can't for instance tell me about gender differences. For that we'd need to make two new class types called "Man" and "Woman". The state and behaviors of these two classes will differ from each other in a lot of ways except for the ones that their inherit from Human.

Therefore inheritance allows us to encompass the parent class' state and behaviors into its child. The child class can then extend the state and behaviors to reflect the differences it represents. The most important aspect of this concept to remember is that the child class is a more specialized version of the parent.

What Is a Superclass?
In the relationship between two objects a superclass is the name given to the class that is being inherited from. It sounds like it's a super duper class but remember that it's the more generic version. Better names to use might be base class or simply parent class.
To take a more real world example this time we could have a superclass called "Person". Its state holds the person's name, address, height and weight and has behaviors like go shopping, make the bed, and watch TV. We could make two new classes that inherit from Person called "Student" and "Worker". They are more specialized versions because although they have names, addresses, watch TV and go shopping, they also have characteristics that are different from each other. Worker could have a state that holds a job title and place of employment whereas Student might hold data on an area of study and an institution of learning.

What Is a Subclass?
In the relationship between two objects a subclass is the name given to the class that is inheriting from the superclass. Although it sounds a little more drab, remember that it's a more specialized version of the superclass. Subclasses can also be known as derived classes or simply child classes.

In the previous example, Student and Worker are the subclasses.

How Many Subclasses Can I Have?
You can have as many subclasses as you want. There is no limitation to how many subclasses a superclass can have. Likewise there's no limitation on the number of levels of inheritance. A hierarchy of classes can be built upon a certain area of commonality.
In fact, if you look at the Java API libraries you will see many examples of inheritance. Every class in the APIs is inherited from a class called java.lang.Object. For example, any time you use a JFrame object you're at the end of a long line of inheritance:
java.lang.Object
 extended by java.awt.Component
 extended by java.awt.Container
 extended by java.awt.Window
 extended by java.awt.Frame
 extended by javax.swing.JFrame

In Java, when a subclass inherits from a superclass its known as "extending" the superclass.

Can My Subclass Inherit From Many Superclasses?
No. In Java, a subclass can only extend one superclass.

Why Use Inheritance?
Inheritance allows programmers to re-use code they've already written. In the Human class example we don't need to create new fields in the Man and Woman class to hold the blood type because we can use the one inherited from the Human class.

Another benefit of using inheritance is it also allows us to treat a subclass as if it was a superclass. For example, let's say a program has created multiple instances of the Man and Woman objects. The program might need to call the sleep behavior for all these objects. As the sleep behavior is a behavior of the Human superclass we can group all the Man and Woman objects together and treat them as if they were Human objects.

No comments:

Post a Comment