|
|
||
| You are here: | ||
|
This is really an HL topic but some aspects of inheritance are useful to SL also. '
The call to super calls the Constructor in the parent Class (TextField).
isEmpty() is a method of IntegerTextField
as is getInteger()
See the exceptions page for more details on this technique.
These simply call the corresponding constructors of the TextField superclass.
super can also be used to access the methods of the parent class.
A null String instance is returned if the binary input value is not valid.
A private helper method of the class. Check that a byte has 8 bits
Check each of the 8 bit positions in turn for a 1 or a 0. Notice that the custom exception message can contain data too - here it says where the bad digit was found in the String.
Notice that the binary String must be of length 8 to work properly - this would appear as a pre-condition in a well-designed dossier application. weight keeps track of the column weightings. 10011101 (Most and Least Significant Bits).
|
On this page: [ subclasses | extending the TextField | BinaryTextField ] In this example we extend the capabilities of the TextField Class. So far we have been converting text strings in a text field to numeric form using the construct: int x = Integer.parseInt(someTextField.getText()); At times we have checked the TextField to see of there is any text, for example: String choice = someTextField.getText(); We can create a TextField type that includes these additonal functions as methods while retaining all the functionality of the existing TextFiedl Class by extending it: Let's call it IntegerTextField and build in the required methods: /** This Class can now be used in an Applet (or other application): import java.applet.Applet; There may not be a huge apparent benefit for re-writing the AddTwo Applet in this way but for more complex objects that are frequently re-used it saves time and is more reliable (test the Class once and re-use as many times as you want). The next example shows that Exceptions and extended TextField's can be used together to validate data that needs to be input to an application. In this example we are going to developa quiz to test the user's knowledge of decimal to binary conversion. The user will have to type in a binary number. The validation of input will be put into an a BinaryTextField Class which extends the awt.TextField Class. First we build the exception Class: /** Now the BinaryTextField Class, first the constructors: /** Next, here are some methods that we will need to validate the input (for this exercise a binary number must have 8 bits and every digit must be either a 1 or a 0. /** The next page shows the BinaryQuiz Applet which actually makes use of the Classes developed here. Related: [ Java Home | previous:Exceptions| next:Binary Quiz Applet ] |
A key feature of OOP languages is the ability to extend existing classes by giving them extra methods and properties. Such classes have access to the functionality of the parent (or super) class. |
|
|
|||
|
Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: May 31, 2009 |