5 Pillars Of OO: Part 1 Inheritance

5 Pillars Of OO: Part 1 Inheritance

In the world of object-oriented languages and programming, there are 5 main important subjects. The following 5 series of articles will tackle these important subjects.

The 5 Pillars of OO are:

  • Inheritance
  • Encapsulation
  • Overloading & Overriding (Reflection)
  • Polymorphism
  • And a sense of humour

Before we hit the road, all examples will be in Java, and maybe Ruby. Also mentioning some other programming languages.

Inheritance

In the world of object-oriented programming, everything is an object. A person like you and I can be represented as an object. And we as people belongs in a hierarchy of objects. For example, Humans are Mammals. Mammals are animals, and animals are living things. You can see where we are going with this!!

In this example, let's take some more specific example. Cats. Who doesn't like them!! (Some people over-love them if you ask me!!)

In the following code, a cat class is the base class for all other types of cats. Such as lions, tigers. Inheritance help developers reusing codes, and make it less of a hassle than writing the features of such objects over and over again

So enough talking, let's get coding


public class Cat {
    private String name;
    private int speed;
    public final String  SPECIES = "Mammals";

     public Cat(String name, int speed) {
        this.name = name;
        this.setSpeed(speed);
     }
}

A cat has a name, and of course the speed in MPH and the species type. (For now ignore the privates and publics, these are explained in part 2)

As you would expect, all cats have speed, so when we write the lion class, we don't need to worry about that, it is covered!!! And this is the great joy of inheritance, laziness.

So, lets inherit!!!


public class Tiger extends Cat {

    private String tigerType;
    private int age;

    public Tiger(String name, String tigerType, int speed) 
    {
        super(name, speed); //ignore for now
        this.tigerType = tigerType;
    }
}

The keyword `extends` tells the compiler in Java that we want to class tiger to inherit the features and members property of the cat. This way, the tiger has access to all public methods and variables set by the parent class.

Encapsulation Trailer

Just to give you heads up, keywords such as private, public and others give the class power over the privacy of its members and who can access them. This is will be greatly explain better in Part 2.

Let's Continue

The super in the Tiger class allows the tiger to call the parent constructor. Again, for the sake of laziness, save us typing more codes!!!

And there you have it, that's what inheritance is. Help us organise objects, and to do that we categories into types. In Java and Ruby, everything is almost an object where the base class of all classes is the object class.

Proofing a point Tigers and lions are both types of cats, so let's make a lion class that represent that relationship:


public class Lion extends Cat {

    private int age;
    private String lionType;

    public Lion(String name, int age, String lionType, int speed) {
        super(name, speed);
        this.age = age;
        this.lionType = lionType;
    }
}

The lion belongs to the cats family, so tigers and lions share features. And most defiantly you can add more to the cat class to add more common features that belongs to all cats such as tail length

In java, ruby and others, you can only inherit from one other class, so you have what is called one-to-one relationship. However, languages such as C++ offer multi-Inheritance but it does get messy!! (Or I have been told!)

Further Note:

When working with complex projects, and when solving problems, OO programming give us an easy way to face issues by splitting them into smaller ones. But inheritance on its own not so much powerful, to see how it all fits into the big picture, read part 2.

AbdulRehman Faraj

Electronic and Computer Engineer graduate and currently working as Development and Integration Analyst at Baring Asset Management. Love Programming, and game development....

London, United Kingdom http://github.com/AbdulR3hman