chapter_12_beginning_javabook

The first method allows a date to be shown so that

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: w methods. The first method allows a date to be shown so that the month is expressed with letters instead of numbers. The second method prints the date in the American format, also in the case when the object is created with the European format specifier. In addition to these new methods, it is possible to call the old class Date methods for BetterDate objects. In one respect class BetterDate is worse than class Date. Because there is only one constructor method in class BetterDate, there remains only the following way to create BetterDate objects: BetterDate date_of_reunification_of_germany = new BetterDate( "03.10.1990"); The constructor of BetterDate only accepts strings as its parameter. Date objects can also be constructed by supplying the initialization date as values of type int: Date date_of_independence_of_finland = new Date( 6, 12, 1917 ) ; The sole constructor of class BetterDate passes the string that it receives as a parameter to the constructor of class Date. The constructor consists of the lines public BetterDate( String date_as_string ) { super( date_as_string ) ; } This constructor calls the constructor of class Date with the help of the super keyword. The keyword super refers to the constructor of the superclass. It is thus the constructor of class Date that actually processes the string and extracts the date information from it. Only the constructor of the immediate superclass can be called by using the super keyword. If there are higher classes above the immediate superclass, their constructors cannot be accessed in this way. In general, it is possible to declare the constructor of a derived class in two ways. The first way is to call the constructor of the immediate superclass before the other statements of the constructor: public DerivedClass( declarations of formal parameters ) { super( actual parameters to the constructor of superclass ) ; zero or more other statements } The second possibility is to write the constructor without an explicit call to the constructor of the superclass: public DerivedClass( declarations of formal parameters ) { zero or more statements } In the first case above, the constructor of the superclass is executed before the constructor of DerivedClass. In the second case, where the explicit call is missing, the compiler generates an implicit call to the default constructor of the superclass before executing the body of the constructor of De...
View Full Document

Ask a homework question - tutors are online