Java Reference
In-Depth Information
Asubclass can override (replace) aninherited method sothat thesubclass'sversion
ofthemethodiscalledinstead. Listing2-23 showsyouthattheoverridingmethodmust
specify the same name, parameter list, and return type as the method being overridden.
Listing 2-23. Overriding a method
class Vehicle
{
private String make;
private String model;
private int year;
Vehicle(String make, String model, int year)
{
this.make = make;
this.model = model;
this.year = year;
}
void describe()
{
System.out.println(year+" "+make+" "+model);
}
}
class Car extends Vehicle
{
private int numWheels;
Car(String make, String model, int year, int numWheels)
{
super(make, model, year);
}
void describe()
{
System.out.print("This car is a "); // Print without
newline - see Chapter 1 .
super.describe();
}
public static void main(String[] args)
{
 
Search WWH ::




Custom Search