Extends Java Multiple classes

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.

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.

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.

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();

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();

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?


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 Reply

Your email address will not be published. Required fields are marked *