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: ived classes The toString() method is called automatically for a BetterDate object when operator + is used as the string concatenation operator. 387 Objects of derived classes are created in the normal way. We cannot see anything here indicating that BetterDate is derived from class Date. class BetterDateTester { public static void main( String not_in_use ) { BetterDate birthday_of_einstein = new BetterDate("14.03.1879"); System.out.print( "\n Albert Einstein was born on " + birthday_of_einstein ) ; birthday_of_einstein.increment() ; System.out.print( "\n Albert was one day old on " + birthday_of_einstein.to_string_with_month_name() ) ; birthday_of_einstein.increment() ; System.out.print( "\n Albert was two days old on " + birthday_of_einstein.to_american_format_string() ) ; } } This statement is, for example, "hard evidence" confirming the fact that inheritance has indeed taken place. The object reference birthday_of_einstein is pointing to a BetterDate object, and method increment() is invoked for the object. Because the BetterDate class does not contain a method named increment(), the method must have been inherited from class Date. - 2. Using a BetterDate object. D:\javafiles3>java BetterDateTester Albert Einstein was born on 14.03.1879 Albert was one day old on March 15, 1879 Albert was two days old on 03/16/1879 - X. Dates printed with different methods. 388 Chapter 12: Inheritance and class hierarchies Program is another example where a new class is derived from class Date. The name of the class is CurrentDate. Classes Date and CurrentDate only differ from each other in that they have different kinds of constructors. The constructor of class CurrentDate reads the date information that is maintained by the operating system of the computer. The constructor gets this information by using the standard Java classes Calendar and GregorianCalendar. The used standard method gets the computer’s date by interacting with the operating system of the computer which, in turn, interacts with the clock electronics of the computer. Programs and are examples in which objects of class CurrentDate are created and used. When a CurrentDate object is created, its date is the current computer’s date. It is, nonetheless, possible to change the init...
View Full Document

This document was uploaded on 01/20/2014.

Ask a homework question - tutors are online