OOP Concept: Inheritance

Inheritance is the process by which objects of one class acquire the properties of objects of another class.


Inheritance supports the concept of hierarchical classification. In inheritance, each derived class shares common characteristics with the class from which it is derived. In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an exiting class without modifying it. We can achieve this by deriving a new class from the existing one. The new class will have the combined features of both the classes. Each sub-class defines only those features that are unique to it.

public class Bird {
    private String color;
    private String food;
    private String movement;
    public Bird(String color, String food, String movement) {
        this.color = color;
        this.food = food;
        this.movement = movement;

    public String toString() {
        return "Bird {" +
                "color='" + color + '\'' +
                ", food='" + food + '\'' +
                ", movement='" + movement + '\'' +

public class FlyingBird extends Bird {
    public FlyingBird(String color, String food) {
        super(color, food, "flying");

public class NonFlyingBird extends Bird {
    public NonFlyingBird(String color, String food) {
        super(color, food, "walking");

public class Robin extends FlyingBird {
    public Robin(String color) {
        super(color, "fruit");

Declaring subclasses

public class B extends A {

  • means class B is a specialization of a class A
  • the is a relationship exits
  • a B object is an A object

Other Names:

  • Super class is also called “Parent class”
  • Sub class is also called “Child class”

Child class inherits characteristics of parent class:

  • attributes
  • methods

So when we say public class Robin extends FlyingBird { .. } then

  • a Robin object inherits all FlyingBird attributes
  • which, in turn, inherits Bird attributes

Remember, extends is unidirectional. A extends B does not imply that B extends A. For example:

Bird aBird = new Robin(“Blue”);

  • is legal
  • a Robin object is a Bird object

But Robin aRobin = new Bird(“Grey”, “Fruit”, “Flying”);

  • is not legal
  • a Bird object not necessarily a Robin object
OOP Concept: Inheritance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s