Page 302 - Introduction to Programming with Java: A Problem Solving Approach
P. 302

                268 Chapter 7 Object-Oriented Programming—Additional Details
 /***************************************************************
*
Car4Driver.java
Dean & Dean
This class is a demonstration driver for the Car4 class.
***************************************************************/
*
*
*
public class Car4Driver
{
}
public static void main(String[] args)
{
  Car4 allexCar = new Car4("Porsche", 2006, "beige");⎫ ⎬
Car4 latishaCar = new Car4("Saturn", 2002, "red"); ⎭ System.out.println(allexCar.getMake());
 } // end main
// end class Car4Driver
Output:
Porsche
constructor calls
 Figure 7.14 Car4Driver class which drives the Car4 class in Figure 7.13 Apago PDF Enhancer
See the Employee2 program in Figures 7.16a and 7.16b. The driven class in Figure 7.16a compiles suc- cessfully, but the driver in Figure 7.16b generates a compilation error. As in Figure 7.15a, the driver code in Figure 7.16b calls a zero-parameter constructor. It worked before, so why doesn’t it work this time? This time, the driven class in Figure 7.16a explicitly defines a constructor, so Java does not provide a default zero- parameter constructor. And without that constructor, the compiler complains that there’s no matching con- structor for the zero-parameter constructor call. How can you fix the Employee2 program to get rid of this error? Add the following zero-parameter Employee2 constructor to your Employee2 class:
public Employee2()
{}
 public class EmployeeDriver
{
public static void main(String[] args)
  {
 Employee emp = new Employee();
emp.readName();
} // end main
} // end class EmployeeDriver
zero-parameter constructor call
Figure 7.15a Driver for Employee program































































   300   301   302   303   304