Extends Java Multiple classes

Extends Java

An OOPs language is meant more for reusability. It achieves reusability with "has-a" relation (through composition) and "is-a" relation (through inheritance). For "has-a" relationship, known as composition in Java, refer Composition – "has-a" Relationship. Now let us discuss inheritance and how the reusability is achieved. To get the concept, very small programs are given.

Before going into the discussion, you are expected to know what a class is in Java and the syntax of writing a Java class. This you can get from What is a class in Java? and if you would like to know from the beginning know from, Java for Beginners.

Observe the following to classes.

public class Bird
{
  public void walk()
  {
    System.out.println("Bird can walk");
  }
}

public class Ostrich
{
  public void run()
  {
    System.out.println("Ostrich can run at good speeds");
  }
}

Now observe, the Bird and Ostrich are two different classes which are in no way connected. Both are practically disjoint. One does not know the other. Now let us go into our topic.

Because Ostrich is a bird and also can walk, I must write a method called walk() in Ostrich class because Ostrich does not have the method. Yes, you and everyone accept. OOPs comes into play here. OOPs is meant for reusability, I told earlier. OOPs says, do not write, I give a solution for it.

OOPs says, connect Ostrich with Bird so that Ostrich can use Bird’s walk() method (like a son is connected to father, so that son can make use of father’s property). Idea is good, but how to do it programmatically. Java say, use "extends" keyword and the problem is solved.

The above two classes are modified as follows just with an addition of extends Java keyword.

public class Bird
{
  public void walk()
  {
    System.out.println("Bird can walk");
  }
}

public class Ostrich extends Bird
{
  public void run()
  {
    System.out.println("Ostrich can run at good speeds");
  }
}

Observe, "Ostrich extends Bird". When Ostrich extends Bird class, Ostrich can make use of all the property of Bird like variables, methods and constructors (right now we are having methods only here). Just for execution purpose, I add one main() method in Ostrich class.

class Bird
{
  public void walk()
  {
    System.out.println("Bird can walk");
  }
}

public class Ostrich extends Bird
{
  public void run()
  {
    System.out.println("Ostrich can run at good speeds");
  }
  public static void main(String args[])
  {
    Bird b1 = new Bird();         // create a Bird object
    b1.walk();                    // Bird object b1 is calling its own walk() method

    Ostrich o1 = new Ostrich();   // create a Ostrich object
    o1.run();                     // Ostrich object1 o1 is calling its own run() method
    o1.walk();                    // now Ostrich object1 o1 is calling Bird’s walk() method. THIS IS THE POWER OF INHERITANCE
  }
}

Extends Java
Now let us watch the code.

public class Ostrich extends Bird

The disjoint two classes are joined with extends keyword. That is, "extends" joins or links two classes in Java. The advantage of linking is now the Ostrich can make use of Bird’s methods. This is discussed down.

Bird b1 = Bird();
b1.walk();

Nothing great in the above code, as Bird object b1 is calling its own walk() method (like you are eating from your own plate). Observe the following code.

Ostrich o1 = new Ostrich();
o1.run();
o1.walk();

Ostrich object o1 is calling its own method run() and is no great. The greatness comes now. Observe, the Ostrich object o1 is calling walk() method of Bird (like you are eating from other’s place which is not permitted by the other). Here object belongs to Ostrich and method belongs to Bird. That is, walk() method of Bird is used by Ostrich. This avoids writing again walk() method in Ostrich class (because Ostrich also wants to walk). This we call as "code reusability".

Shall I go a little further? Does the following is works?

b1.run();

The strange thing here is the Bird object is calling Ostrich method run(). No it is not possible (like a father does not have right over son’s property).

Inheritance using extends Java

Now let us add some technical words (for nothing but to confuse you, but no escape). When Ostrich extends Bird, there establishes a relation between them (earlier not as they are disjoint). The relation we call as "inheritance", one of the OOPs concepts, the other being encapsulation and polymorphism. To inherit we used extends keyword. The class which extends is known as subclass and the class which is getting extended is known as super class. In the above code, Ostrich is subclass and Bird is super class.

The rule of extends Java says, "subclass can make use of super class members but super class cannot make use of subclass members". That is, Bird cannot make use of Ostrich methods.

Now, I am sure you are clear of what is inheritance, extends, super class and subclass. Any query may be posted to me in the comment box down.

Note: Would you like to know a how super class can call subclass methods? It is very secret. To know the secret click here (which is possible through object casting that leads to polymorphism).

Leave a Comment

Your email address will not be published.