The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search

Trail: Learning the Java Language
Lesson: Objects and Classes in Java

Extending a Class

If you look at the online javadoc for Applet(in the API reference documentation), you can see a long list of methods implemented by that class. All of these methods are inherited by Spot. Also, if you look at Applet's ascendants, you will notice that Applet descends from a long line of auspicious classes: Panel, Container, Component, and finally, Object. Hence, Applet, and consequently, Spot, inherit a large number of variables and methods from these classes that, among other things, manage the space in which the applet runs.

Inheritance provides one of the premier benefits of object-oriented programming: code reuse. This is shown here. With a tiny amount of code, Spot actually implements a fairly complex program (Spot reserves space in an HTML page, is started and stopped as the browser instructs, handles mouse click events, and draws in its area).

Being a subclass comes with responsibilities as well. Depending on the superclass, a subclass may be required to implement certain methods or be expected to override some. One responsibility that comes with subclassing Applet is that the subclass must implement at least one of these methods: init, start, or paint. Spot implements both init and paint but does not implement start. You should fully understand the parent class when creating a subclass of it. To find out what a subclass inherits from its parents, what it must override, what it cannot override, and so on, go to Understanding Inheritance. To find out more about the benefits and responsibilities of subclassing Applet, go to the next trail, Writing Applets(in the Learning the Java Language trail).


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search