Programming2(CS272)
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
class A class B extends A B b1=new B();
{ { System.out.println(b1 instanceof B);//true
} } System.out.println(b1 instanceof A);//true
05:26 2
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
class A class B extends A B b1=new B();
{ { System.out.println(b1 instanceof B);//true
} } System.out.println(b1 instanceof A);//true
05:26 3
Example public class Test {
public abstract class Shape{ public static void main(String[] args) {
public abstract double getArea(); Shape shape1 = new Circle(2);
} Shape shape2 = new Rectangle(3, 4);
displayObject(shape1);
public class Circle extends Shape{ displayObject(shape2);
private double radius; }
public Circle( double radius) public static void displayObject(Shape shape) {
{
System.out.println(“area=“+shape.getArea());
this.radius=radius;
}
public double getRadius(){return radius;} if (shape instanceof Circle) {
Public double getArea(){ Circle c=(Circle)shape;
return 3.14*radius*radius;} System.out.println("The circle radius is "
} +c.getRadius());
public class Rectangle extends Shape{ }
private double width; else if (shape instanceof Rectangle) {
private double height; Rectangle rect=(Rectangle)shape;
public Rectangle(double width,double height){ System.out.println("The rectangle Width is "
this .width=width; +(rect.getWidth()));
this .height=height;
}
System.out.println("The rectangle height is "
public double getWidth(){return width;} +(rect.getHeight()));
public double getHeight(){return height;} }
public double getArea() }
{return width*height;}
05:26 } 4
}
object.equals()
Method equals in class Object offers only a trivial comparison, as it compares whether the
2 reference for the 2 objects are equal and not the contents of the 2 objects.
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.
//********************************************************************
abstract public class StaffMember
{
protected String name;
protected String address;
protected String phone;
//-----------------------------------------------------------------
// 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";
result += "Address: " + address + "\n";
result += "Phone: " + phone;
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.
//********************************************************************
public class Volunteer extends StaffMember
{
//-----------------------------------------------------------------
// Constructor: Sets up this volunteer using the specified
// information.
//-----------------------------------------------------------------
public Volunteer (String eName, String eAddress, String ePhone)
{
super (eName, eAddress, ePhone);
}
//-----------------------------------------------------------------
// Returns a zero pay value for this volunteer.
//-----------------------------------------------------------------
public double pay()
{
return 0.0;
}
}
//********************************************************************
// Employee.java Author: Lewis/Loftus
//
// Represents a general paid employee.
//********************************************************************
public class Employee extends StaffMember
{
protected String socialSecurityNumber;
protected double payRate;
//-----------------------------------------------------------------
// 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();
result += "\nSocial Security Number: " + socialSecurityNumber;
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.
//********************************************************************
public class Executive extends Employee
{
private double 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);
bonus = 0; // bonus has yet to be awarded
}
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.
//********************************************************************
public class Hourly extends Employee
{
private int hoursWorked;
//-----------------------------------------------------------------
// 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();
result += "\nCurrent hours: " + hoursWorked;
return result;
}
}