|
|
Objects, Classes, and Interfaces |
A subclass can either completely override the implementation for an inherited method or the subclass can enhance the method by adding functionality to it.Replacing a Superclass's Method Implementation
Sometimes, you want a subclass to entirely replace its superclass's implementation of a method. Indeed, many superclasses provide an empty method implementation with the expectation that most, if not all, subclasses will completely replace the superclass's implementation of the method.One example of this is the
runmethod in theThreadclass. TheThreadclass provides an empty implementation (the method does nothing) of therunmethod because, by definition, therunmethod is dependent on the subclass implementation of the method. TheThreadclass can't possibly provide a reasonable default implementation for therunmethod. However, therunmethod cannot be abstract because it also does not make sense for theThreadclass to be abstract (programmers should be able to instantiate a genericThreadwithout building a subclass). Thus, the implementation ofrunis empty.To completely replace a superclass's method implementation, simply name your method the same as the superclass method and provide the overriding method with the same signature as the overriden method:
Theclass BackgroundThread extends Thread { void run() { . . . } }BackgroundThreadclass overrides therunmethod from its superclassThreadand completely replacesThread's implementation of it.Adding to a Superclass's Method Implementation
At other times a subclass will want to keep its superclass's implementation of a method but enhance it further with behavior specific to the subclass. For example, constructor methods within a subclass typically do this--the subclass wants to preserve the initialization done by the superclass, but provide additional initialization specific to the subclass.Suppose that you wanted to create a subclass of the
Windowclass in thejava.awtpackage. TheWindowclass has one constructor that requires aFrameargument which is the parent of the window:This constructor performs some initialization on the window such that it will work within the window system. To make sure your new subclass ofpublic Window(Frame parent)Windowalso works within the window system, you too must provide a constructor for yourWindowsubclass that performs the same initialization. Rather than attempt to figure out and recreate the initialization process that occurs within theWindowconstructor, you would much rather just use what theWindowclass already does. You can leverage the code in theWindowconstructor simply by calling it from within yourWindowsubclass constructor:Theclass MyWindow extends Window { public MyWindow(Frame parent) { super(parent); . . . // MyWindow-specific initialization here . . . } }MyWindowconstructor calls the superclass's constructor first, before it does anything else. Typically, this is the desired behavior in constructors--the superclass should get the opportunity to perform all its initialization before the subclass. Other types of methods may wish to call the superclass's implementation of the method at the end of the subclass's method or in the middle of it. If the positioning of the call to the superclass's method is critical to the successful operation of the subclass's method, it's important to note that in a comment.Methods a Subclass Cannot Override
- A subclass cannot override methods that are declared
finalin the superclass (by definition, final methods cannot be overriden). If you attempt to override a final method, the compiler will display an error message similar to this one and refuse to compile the program:FinalTest.java:7: Final methods can't be overriden. Method void iamfinal is final in class ClassWithFinalMethod. void iamfinal() { ^ 1 error
For a discussion of final methods, see Writing Final Classes and Methods.- Also, a subclass cannot override methods that are declared
staticin the superclass. In other words, a subclass cannot override a class method. See Instance and Class Members for an explanation of class methods.Methods a Subclass Must Override
Subclass must override methods that are declaredabstractin the superclass, or the subclass itself must be abstract. Writing Abstract Classes and Methods discusses abstract classes and methods in detail.
|
|
Objects, Classes, and Interfaces |