Java Tutorial: The “extend” Keyword

, ,

In Java, there's the very important “extend” keyword.

A class declaration can use the extend keyword on another class, like this: class C extends B { … }.

When a class C extends class B, C automatically has all variables and methods defined in class B. (think of it as automatic copy/paste system.)

If class C defines a variable or method that has the same name in class B, class C's definition overrides B's. Here's a example:

class B {
    int x;
    void setIt (int n) { x=n;}
    void increase () { x=x+1;}
    void triple () {x=x*3;};
    int returnIt () {return x;}

class C extends B {
    void triple () {x=x+3;} // override existing method
    void quadruple () {x=x*4;} // new method

public class GetRich {
    public static void main(String[] args) {
        B b = new B();
        System.out.println( b.returnIt() ); // prints 9

        C c = new C();
        System.out.println( c.returnIt() ); // prints 6

In the above code, class C inherits all class B's variables and methods. Class C overrides the “triple” method, and added a “quadruple” method.

Now, other classes can extend your Class C, and inherit all members and methods of class C (which includes those in B). In this way, a tree hierarchy (tree) is formed.

When class C extends B, we say that C is a “subclass” of B, and B is the “superclass” of C. This is called inheritence, because C inherited from B.

Two or more classes can inherit from the same parent class (i.e. a class can have many children). However, a class can NOT inherit from multiple classes. (i.e. each class can only have one parent.)

In Java, EVERY class is a subclass of java.lang.Object (or subclass of its subclasses). Every class in Java is a node on this giant tree.

The Role of OOP'S Inheritance in Software Engineering

This inheritance concept in Object Oriented Programing is a milestone in software engineering. OOP could not have achieved any status without inheritance. Inheritance is the culprit of the doctrine that OOP creats code re-use and will be a panacea in software industry.

Just imagine, every element of software in the universe, are made of objects, which come with a complete set of subroutines to deal with itself, and every new software will just pluck from a giant tree to inherit all its fruits and become a new branch. That is the rapture of the OOP fad.

In practice, very few software problems form a nice tree, and the Object oriented view and inheritance themselves created many complexities. Among which we've seen is instance members vs class members , and the forcing of a inflexible and artificial tree. There are also complications of abstract classes and methods, interface, “polymorphism”, multi-inheritance… and in resolving these is latest “Aspect Oriented Programing”. The baroque machinery and ideas (as in Java) to carry out software with OOP and inheritance also set the ideology back.

For a detailed exposition on OOP's complexities, see: What are OOP's Jargons and Complexities.

blog comments powered by Disqus