Java Tutorial: The “extend” Keyword

Xah Lee, 2005-02

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 a internal copying mechanism)

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

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();
        b.setIt(2);
        b.increase();
        b.triple();
        System.out.println( b.returnIt() ); // prints 9

        C c = new C();
        c.setIt(2);
        c.increase();
        c.triple();
        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.

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

When class C extends B, we say that C is a “subclass” of A, and A 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. However, a class can only have one parent. In other words, in Java, you cannot subclass from multiple classes.

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.

Reference: Java Doc: overview-tree↗.

Reference: Java Tutorial: subclasses↗.

Reference: Java Lang Spec: classes↗.

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 a 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.


Page created: 2005-01.
© 2005 by Xah Lee.
Xah Signet