What is Inheritance?
Home ] Up ] [ What is Inheritance? ] Inheritance versus Containment ] Superclasses & Subclasses ] Visibility Modifiers & Inheritance ] Final Classes & Methods ] Polymorphism ] Data Hiding & Encapsulation ] Abstract Classes ] Interfaces ] Cloning Objects ] The Collections Framework ] Interfaces & Callbacks ]

 

 

Why is Inheritance important?

Have you ever written some code, and then later found that you needed to write something that was almost, but not quite, the same?

What did you do?

  • Cut and paste the code, and then change it to suit?
  • Modify the existing code so it could handle both cases?
  • Why isn't either of those solutions a good idea?

Inheritance provides a mechanism to allow you to extend existing code in a natural way.

Classifications, Taxonomies, and Inheritance

Humans are always classifying things!

Classification is a way of extracting out the common attributes of several like objects, and leaving only the differences.

Classification enables us to deal with new things in terms of what is already familiar to us; reduces volume of information; helps simplify.

We often call such classifications taxonomies. Taxonomies are everywhere -- for example:

Here are some more:

Inheritance

Inheritance is an abstraction mechanism which allows classes to be related hierarchically.

  • Allows classes to share definitions

  • Makes commonality of code explicit.

For example, consider the problem of drawing different kinds of graphical shapes. We can extract the common attributes and behaviors of any kind of shape, and encapsulate it in a Shape class:

The Circle, Triangle and Square classes inherit (or derive) their behaviors and properties from class Shape.

 

This page was last modified on 02 October, 2007