Java Reference
In-Depth Information
Programmers note:
The signature of a method is its unique name and parameter list.
Since all constructors have the same name as the class, the construc-
tors can only be identified by their unique parameter list. Overloaded con-
structors allow building objects in different ways. Java determines which
constructor to use by looking at the object's parameters. Once you define
a constructor, no matter its signature, the default constructor is not used
in creating objects.
The following program, named PayrollDemo.java, shows the use of
overloaded constructors.
On the Web
ThesourcefileforthePayrollDemo.javaprogramfilecanbefoundin
theChapter15folderat www.crcpress.com .
//***************************************************************
// File name: PayrollDemo.java
// Reference: Chapter 15
//***************************************************************
// Topics:
// 1. A class with object and class attributes
// and object and class methods
// 2. Creating and using constructors
// 3. Polymorphism by overloaded constructors
//**********************************************************
//******************************************
//******************************************
// Employee class
//******************************************
//******************************************
class Employee
{
//********************************
// attributes section
//********************************
// field variables
private String name = "no name";
private String address;
private String ssn = "xxx";
private int dependants = 1;
// Default for dependants field
private int empNum;
// Class attribute (static qualifier)
private static int empCounter = 0;
Search WWH ::




Custom Search