Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
If we didn't have a paint method in our class Override ...
, then the paint method in the class OurApplet would be called.
Override's paint method is CALLED INSTEAD,
because it OVERRIDES OurApplet's .
Our class Override
is an OurApplet
is an Applet
is a Panel
is a Container
is a Component
is an Object.
This paint method overrides the one in
OurApplet, that overrides the one in
Container, that overrides the one in
If none of the classes had a paint method except Component, then the one in Component would be called.
Often, you will want to extend the functionality of overridden methods, not replace them entirely.
We illustrate this by calling the paint method in Override's superclass OurApplet.
If the browser makes a call to Applet's init method, the one in Override is called instead.
We have overridden the one in OurApplet, that overrides the one in Applet.
In it ;) , we first call the init method in the superclass OurApplet.
A similar specification of the methods : same name, same parameters, is required for overriding.
Same spec as the one in the Applet class.
You can have an array like this ...
, and fill it with different class types, for every class derives from Object .
Most classes each have their own toString method, that returns a String representing the object.
They override the toString method in the Object class.
To call these DIFFERENT toString methods, Color's, Date's, etc..., you can do something like this.
Or even like so, because toString is called here, too, for conversion to String.
If you want the same mechanism to apply to your own classes, just add toString methods to them.
The right method being called automatically, depending on the actual type of the object :
If you're familiar with C++, then this is about virtual functions. In Java you don't have to do anything special to accomplish this.
To prevent methods from being overridden in subclasses, use the keyword final
final void somemethod( ) ...
You can use this keyword also to prevent a class from ever being extended altogether.
final class Claus ...
Variables can be overridden, too . Can be tricky and obfuscating .