concept override in category java

appears as: override, overrides
JavaFX in Action

This is an excerpt from Manning's book JavaFX in Action.

The class overrides the getName() method to provide its own answer, which explains the override keyword prefixing the function. The extra keyword is required at the start of any function that overrides another; it doesn’t do anything other than help document the code and make it harder for bugs to creep into our programs. If you leave it off, you’ll get a compiler warning. If you include it when you shouldn’t, you’ll get a compiler error.

You should use the override keyword even when subclassing Java classes, which is why you may have spotted it on the toString() function of Animal, in listing 3.8. All objects in the JVM are descendants of java.lang.Object, which means all JavaFX objects are too, even if they don’t explicitly extend any class. Thus toString(), which originates in Object, needs the override keyword.

Class inheritance (abstract, extends, override)

JavaFX Script supports inheritance, using the extends keyword. As in Java, functions are virtual. The abstract keyword can be used to prevent a class from being instantiated directly. The override keyword is needed on any instance function or variable that overrides a parent class; overridden variables are used to change initial values.

import java.util.Date;

abstract class Animal {
var life:Integer = 0;
var birthDate:Date;

function born() : Void {
this.birthDate = Date{};
}
function getName() : String {
"Animal"
}
override function toString() : String {
"{this.getName()} Life: {life} "+
"Bday: {%te birthDate} {%tb birthDate}";
}
}
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest