lect05-part1-week3
lect05-part1-week3
Lecture 5
1
lect4
Object Casting
Syntax:
To cast the reference a1 to be of type B Casting checked twice
A a1=new B(); At compilation : class B is sub class of A
……. (otherwise compile error)
………. At runtime: a1 points to an object of class B
B b=(B)a1; //casting (otherwise runtime error:classCastExeption)
Instanceof Operator
Instanceof is a a boolean java operator that returns true or false
Usage: reference instanceof class
returns true if the reference points to an object of this class or any of its subclasses
Usage: reference instanceof interface
returns true if the reference points to an object of a class implementing the interface
Instanceof Operator
Instanceof is a a boolean java operator that returns true or false
Usage: reference instanceof class
returns true if the reference points to an object of this class or any of its subclasses
Usage: reference instanceof interface
returns true if the reference points to an object of a class implementing the interface
public class A{
i=3
private int i;
j=4
private int j;
public A(int i,int j)
{
a1
this.i=i; i=3
this.j=j; j=4
}
}
a2
class Test
{
public static void main (String[] arg){ false
A a1=new A(3,4);
A a2=new A(3,4);
System.out.println(a1.equals(a2));
}}
5
Overriding object.equals()
class A
{
int i; class Test
int j; {
A(int i,int j)
public static void main (String[] arg)
{
{
A a1=new A(3,4);
this.i=i;
A a2=new A(3,4);
this.j=j;
System.out.println(a1.equals(a2));
}
}
}
public boolean equals(Object obj)
{ true
A a2=(A)obj;
if ( (i==a2.i)&&(j==a2.j) )
return true;
else
return false;
}}
6
Case Study: Simple Payroll System
Consider the class hierarchy shown in Figure. The classes in it
represent various types of employees that might be employed at a
particular company..
StaffMember
Volunteer Employee
Executive Hourly
05:26 7
//********************************************************************
// StaffMember.java Author: Lewis/Loftus
//
// Represents a generic staff member.
//********************************************************************
//-----------------------------------------------------------------
// Constructor: Sets up this staff member using the specified
// information.
//-----------------------------------------------------------------
public StaffMember (String eName, String eAddress, String ePhone)
{
name = eName;
address = eAddress;
phone = ePhone;
}
continue
continue
//-----------------------------------------------------------------
// Returns a string including the basic employee information.
//-----------------------------------------------------------------
public String toString()
{
String result = "Name: " + name + "\n";
return result;
}
//-----------------------------------------------------------------
// Derived classes must define the pay method for each type of
// employee.
//-----------------------------------------------------------------
public abstract double pay();
}
//********************************************************************
// Volunteer.java Author: Lewis/Loftus
//
// Represents a staff member that works as a volunteer.
//********************************************************************
//-----------------------------------------------------------------
// Returns a zero pay value for this volunteer.
//-----------------------------------------------------------------
public double pay()
{
return 0.0;
}
}
//********************************************************************
// Employee.java Author: Lewis/Loftus
//
// Represents a general paid employee.
//********************************************************************
//-----------------------------------------------------------------
// Constructor: Sets up this employee with the specified
// information.
//-----------------------------------------------------------------
public Employee (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone);
socialSecurityNumber = socSecNumber;
payRate = rate;
}
continue
continue
//-----------------------------------------------------------------
// Returns information about an employee as a string.
//-----------------------------------------------------------------
public String toString()
{
String result = super.toString();
return result;
}
//-----------------------------------------------------------------
// Returns the pay rate for this employee.
//-----------------------------------------------------------------
public double pay()
{
return payRate;
}
}
//********************************************************************
// Executive.java Author: Lewis/Loftus
//
// Represents an executive staff member, who can earn a bonus.
//********************************************************************
//-----------------------------------------------------------------
// Constructor: Sets up this executive with the specified
// information.
//-----------------------------------------------------------------
public Executive (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
continue
continue
//-----------------------------------------------------------------
// Awards the specified bonus to this executive.
//-----------------------------------------------------------------
public void awardBonus (double execBonus)
{
bonus = execBonus;
}
//-----------------------------------------------------------------
// Computes and returns the pay for an executive, which is the
// regular employee payment plus a one-time bonus.
//-----------------------------------------------------------------
public double pay()
{
double payment = super.pay() + bonus;
bonus = 0;
return payment;
}
}
//********************************************************************
// Hourly.java Author: Lewis/Loftus
//
// Represents an employee that gets paid by the hour.
//********************************************************************
//-----------------------------------------------------------------
// Constructor: Sets up this hourly employee using the specified
// information.
//-----------------------------------------------------------------
public Hourly (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
hoursWorked = 0;
}
continue
continue
//-----------------------------------------------------------------
// Adds the specified number of hours to this employee's
// accumulated hours.
//-----------------------------------------------------------------
public void addHours (int moreHours)
{
hoursWorked += moreHours;
}
//-----------------------------------------------------------------
// Computes and returns the pay for this hourly employee.
//-----------------------------------------------------------------
public double pay()
{
double payment = payRate * hoursWorked;
hoursWorked = 0;
return payment;
}
continue
continue
//-----------------------------------------------------------------
// Returns information about this hourly employee as a string.
//-----------------------------------------------------------------
public String toString()
{
String result = super.toString();
return result;
}
}