CSCI 1301 October 4, 2001 Lecture 14 Subclasses III - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CSCI 1301 October 4, 2001 Lecture 14 Subclasses III

Description:

polymorphism (biology): the occurrence of different forms, stages or types in ... Dictionary. various CS Books. Lecture 14. 4. Polymorphism revisited ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 20
Provided by: ValuedGate1945
Category:

less

Transcript and Presenter's Notes

Title: CSCI 1301 October 4, 2001 Lecture 14 Subclasses III


1
CSCI 1301 October 4, 2001Lecture 14Subclasses
III
  • Topics for today
  • Announcements
  • More on Polymorphism
  • Class Hourly
  • Class Heirarchy (Class Object)
  • toString equals

2
Announcements
  • Reading Chapter 4, ProgramLive
  • Assignment 3 - due TODAY!
  • Check Web page Notices Daily! There have been
    and will be some office hour changes among the
    TAs

3
Polymorphism
Dictionary
poly (prefix) more than one many morph
(suffix) form shape polymorphism (biology)
the occurrence of different forms, stages or
types in individual organisms of the same species.
various CS Books
polymorphism the ability to determine which of
several operations with the same name is
appropriate. (Dale, Weems, Headington) polymorphis
m selecting a method among several methods that
have the same name on the basis of the actual
types of the implicit parameters.
(Horstman) polymorphism an object-oriented
technique by which a reference that is used to
invoke a method can result in different methods
being invoked at different times. A Java
reference can point to any type of object in its
inheritance ancestry, and all Java method
invocations are polymorphic in that they invoke
the method of the object type, not the reference
type. (Lewis, Loftus)
4
Polymorphism revisited
When calling a method on an object, there are two
things to consider 1. Is it legal? (will it
compile) 2. Which method will be called?
(polymorphism)
at run time
Question1 is it legal RULE If we are using
a superclass reference (the apparent class), the
ONLY methods that can be called legally using
this variable are those that exist in the
superclass. This is the case even if the
superclass variable holds the name of a subclass
object (the real class).
5
Question 2 which method will be called RULE
If we are using a superclass reference (the
apparent class) that currently holds the name of
a subclass object (the real class), AND the
method we call in the superclass is overridden by
the subclass (also exists in the subclass), the
subclass method will be called.
  • Example
  • Employee e new Exec()
  • Which methods are accessible as
  • e.ltsomeMethodNamegt
  • 2. Which method does it get

See two RULEs
6
Employee e new Employee(..) Exec v new
Exec() Salaried s new Salaried(..)
e may call any method in Employee, since is of
class Employee, and it stores the name of an
Employee. v may call any method in Exec, since v
is of class Exec and stores the name of an Exec.
v may also call any method in Employee, since v
is-a Employee (subclass). s may call any method
in Salaried, since s is of type Salaried. s may
also call any method in Employee, since s is-a
Employee (subclass).
7
With these declarations you need to consider the
rules on the previous pages. The name stored in
the variable is a subclass of the type of the
variable. (The real class is a subclass of the
apparent class.)
Employee v new Exec(..) Employee s new
Salaried()
v may call any method in Employee, since v is of
type Employee. Since it stores the name of an
Exec object, if the method in Employee that it
calls ALSO exists in Exec (is overridden by
Exec), the method that is actually used is the
one in Exec (the overriding one). Likewise for s.
8
class Hourly
// An instance of Hourly contains Hourly's
data public class Hourly extends Employee
private double hourlyWage // pay per hour
private int hoursWorked // hours per year
// Constructor to initialize instance variables
public Hourly(String n, int y, double h, int w)
super(n, y) hourlyWage h hoursWorked w
// description of this Hourly person
public String toString() return
Hourly super.toString() ,
hourly pay hourlyWage , hours per
year hoursWorked // Return hours per
year public int getHoursWorked()
return hoursWorked
// Set hours per year public void
setHoursWorked( int w) hoursWorked
w // Return yearly pay public double
getPay() return hourlyWagehoursWorked
// Return hourly pay public double
getHourlyWage() return hourlyWage
// Set hourly pay public void setHourlyWage(
double h) hourlyWage h // Hourly
file Hourly.java
9
Class Hierarchy
Object Employee
Face JLiveWindow Exec Salaried Hourly
MyJLiveWindow
Class Object, predefined, is automatically the
superclass of every class that does not extend
another class.
10
Class Object has a number of methods, which are
therefore inherited by all classes. Two
particularly useful ones are toString equals
public class Object // a string, which
includes name of this object // and the
name of the class of which this object //
is an instance public String toString( )
// this Object has the same
name as x // that is, this Object refers
to the same Object as x public boolean
equals(Object x) return this x

doesnt print anything useful
tells if the two objects are aliases
11
If your class doesnt say extends, it derives
directly from Object. It therefore inherits all
of Objects methods. These methods are very
non-specific. You may wish to override some of
them.
  • You should always override toString. This is
    necessary if we want a meaningful representation
    of the current object. Anywhere in a program
    where a string representation of the object is
    used, your toString method will be called. If
    you dont have one, it will call the one from
    Object not very informative.
  • You should usually override equals. We have
    not been doing this. The equals from Object
    merely tells us whether or not two variables
    refer to the same object (are aliases). We might
    like to know whether they are distinct, but
    equal objects.

12
the toString method
The toString method returns a string
representation for the object. It is primarily
used for debugging. It is called automatically
if we print the object. If we dont override the
toString method, we see only the name of the
class and the address of the object in memory.
// return description of this Employee public
String toString() return Employee name
year hired hireYear
// return description of this Exec public
String toString() return Exec
super.toString() , salary salary ,
bonus bonus
in class Employee
in class Exec
These override the toString method in Object
the signature and return type match exactly.
13
the equals method
The equals method should be called whenever you
want to compare two objects. (If we simply use
the operator , this tests whether or not the
two references are to the same object that is
aliases.) If we dont override the equals
method, the equals method from Object simply
tests whether the two objects are aliases.
What does it mean to be equal class author
decides!
// this object and object e are equivalent
public boolean equals(Employee e) return
(name e.getName() hireYear
e.getHireYear())
in class Employee
14
Does this override method equals in Object?
// this object and object e are equivalent
public boolean equals(Employee e) return
(name e.getName() hireYear
e.getHireYear())
An equals method that might be placed in class
Employee
// somewhere in a method Employee e1 new
Employee(Hal, 2000) Employee e2 new Employee
(Hal, 2001) if (e1.equals(e2)) System.out.prin
tln(Identical Employees) else System.out.print
ln(Employees are different)
What is printed?
15
Does this override method equals in Object?
// this object and object v are equivalent
public boolean equals(Exec v) return
(super.equals(v) bonus v.getBonus()
salary v.getSalary())
An equals method that might be placed in class
Exec
// somewhere in a method Exec v1 new
Exec(Hal, 1999, 100, 5000) Exec v2 new Exec
(Hal, 1999, 100, 5000) if (v1.equals(v2)) Syst
em.out.println(Identical Execs) else System.o
ut.println(Execs are different)
What is printed?
if (v1 v2) .
is this true?
16
What about this?
// somewhere in a method Employee e new
Exec(Hal, 1999, 100, 5000) Exec v new Exec
(Hal, 1999, 200, 5000) if (e.equals(v)) System
.out.println(Identical) else System.out.printl
n(Different)
What is printed?
Problem The equals methods we wrote for
Employee and for Exec were not overrides, but
overloads (their signatures differed from the
equals method in Object). Therefore, the most
pertinent method may not be the one that gets
called. We may call two objects equal that are
in fact not equal. (This wont be a problem as
long as we never store a subclass reference in a
superclass variable either directly or by a
method call.)
17
Solution For any class in which you place an
equals method, write one that overrides the
equals method in Object (and doesnt just
overload it). The parameter must be an Object.
(This problem did not occur with toString, since
there were no parameters.) However, Object is a
wider type than Exec, or Employee. Therefore a
narrowing cast will be necessary.
// override equals from class Object public
boolean equals (Object x) Exec v (Exec)
x return (super.equals(v) bonus
v.getBonus() salary v.getSalary())
in class Exec
18
Problem What if the argument to the equals
method is not really an Exec, but perhaps a
Salaried? The cast will fail. (Aside, we could
reject equality out of hand if the argument is
not even of the same class as the class we are
in.) How to tell the class of an object???

Solution Use the instanceof operator. It
compares an object to a classname and returns
true if the object is really of that class or can
be cast to that class, and false otherwise.
(Make sure before you cast.)
Employee s new Salaried ( ) Employee e new
Employee() if (s instanceof Employee) if (e
instanceof Salaried) if (e instanceof Exec)
19
public boolean equals(Object x) if (x
instanceof Exec) // is an Exec, so cast
legal Exec v (Exec) x return
(super.equals(v) bonus v.getBonus()
salary v.getSalary()) else // not an Exec
dont bother checking anything else return
false
equals method in class Exec
Employee e new Exec(Hal, 1999, 100,
5000) Exec v new Exec (Hal, 1999, 200,
5000) if (e.equals(v)) System.out.println(Ident
ical) else System.out.println(Different)
What about this now?
What is printed?
Write a Comment
User Comments (0)
About PowerShow.com