Objects and Classes in Java |
If you look at the online javadoc forApplet
, you can see a long list of methods implemented by that class. All of these methods are inherited bySpot
. Also, if you look atApplet
's ascendants, you will notice thatApplet
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
, orpaint
.Spot
implements bothinit
andpaint
but does not implementstart
. 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 subclassingApplet
, go to the next trail, Writing Applets.
Objects and Classes in Java |