Implicit calling means that the call is not visible

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: rivedClass. Implicit calling means that the call is not visible in the source program text, and the compiler generates it automatically. Because the compiler generates these automatic calls to the constructors of upper classes, there usually has to be a default constructor (i.e. a constructor that can be called without supplying parameters) in every class that is to be inherited. 386 Chapter 12: Inheritance and class hierarchies Class BetterDate is better than class Date because it has two additional methods. We can say that class BetterDate is the same as class Date plus the two methods. BetterDate is thus an extended version of class Date. // By writing extends Date after the name of the new class, we can inherit the fields and methods from class Date. class BetterDate extends Date { public BetterDate( String date_as_string ) { super( date_as_string ) ; } public String to_string_with_month_name() { String names_of_months = Constructors are not inherited when a class inherits the members of another class. Constructors need to be written in most cases when new classes are derived from existing classes. Here the constructor of BetterDate is written so that it calls the constructor of class Date. The keyword super refers here to a constructor in the superclass. { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" } ; return ( names_of_months[ this_month - 1 ] + " " + this_day + ", " + this_year ) ; } public String to_american_format_string() { char saved_date_print_format = date_print_format ; date_print_format String 'A' ; string_to_return date_print_format return = = = this.toString() ; saved_date_print_format ; string_to_return ; } } date_print_format is a field in class Date. Because BetterDate inherits class Date, this field can be used in a method of BetterDate as if it was declared in class BetterDate. Method toString() is inherited from class Date, and here it is called for "this" BetterDate object. toString() returns a string representation of "this" date in its current printing format. The method could also be called without the this keyword and the dot operator. - 1: Deriving a new class from an existing class. 12.1 Base classes and der...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online