Lecture 22
Lecture 22
Polymorphism
Adapted from:
1) Building Java Programs: A Back to Basics Approach
by Stuart Reges and Marty Stepp
2) Runestone CSAwesome Curriculum
https://longbaonguyen.github.io
Polymorphism
2
Employee and Lawyer
Suppose we have the following classes. We'll use these classes for the next
few examples in the following slides.
public class Employee{
public double getSalary(){return 50000.0}
public String getVacationForms(){return "pink";}
}
public class Lawyer extends Employee{
public double getSalary(){return 60000.0}
public String getVacationForms(){return "yellow";}
public void sue(){System.out.println("I will see you in court!");}
}
public class Secretary extends Employee{
public void takeDictation(String str){…}
}
public class LegalSecretary extends Secretary{
public void filLegalBriefs(){…}
} 3
Coding with polymorphism
A variable of type T can hold an object of any subclass of T.
– You can call any methods from the Employee class on ed.
one.takeDictation("hi");
//error, even though one holds a Secretary object, one is an
// Employee reference and can only call Employee’s methods.
((Secretary) two).getSalary();
//upcast doesn’t change behavior.
//still LegalSecretary’s version
((Employee) two).getSalary();
//still LegalSecretary’s version
10
Overloading
A class can have many forms of the same method. Methods are said to be
overloaded when there are multiple methods with the same name but a
different signature in the same class.
1.Number of parameters
2.Type of the parameters
3.Order of the parameters
11
Number of Parameters
Methods with the same name can be distinguished by the number of
parameters. For example, below, one parameter vs two parameters.
12
Type of Parameters
Methods with the same name can be distinguished by the type of the
parameters. For example, below, int vs double.
13
Order of Parameters
Methods with the same name can be distinguished by the order of the
parameters. For example, below, (int then double) vs (double then int).
14
Invalid Overloading
Case 1:
public void method1(int c, double d)
{…}
Compile error. Same number, data types and sequence. Methods cannot be
overloaded with just different variable names.
Compile error. Same number, data types and sequence. Even though the
return type is different, this is not valid.
16
Ambiguous Call
The following correctly implements method overloading. However, it is
possible that a method call is ambiguous.
18
Compile-time vs Runtime
Employee Sean = new Secretary();
Sean.takeDictation(“hi”);
//compile-time error, no such method in Employee
Sean.fileLegalBriefs();
// compile-time error, no such method in Employee
Sean.getSalary();
//ok
19
Compile-time vs Runtime
((LegalSecretary) Sean).sue();
//compile-time error,sue() isn’t in LegalSecretary
((LegalSecretary) Sean).fileLegalBriefs();
//runtime error, cast too far down the tree
//the program compiles without errors.
((Lawyer) Sean).sue();
//runtime error, horizontal casting not allowed;
21
Object variables
You can store any object in a variable of type Object.
Object o1 = new Point(5, -3);
Object o2 = "hello there";
Object o3 = new Scanner(System.in);
22
Recall: comparing objects
The == operator does not work well with objects.
• == compares references to objects, not their state. It only
produces true when you compare an object to itself.
p2 x 5 y 3
...
23
The equals method
The equals method compares the state of objects.
if (str1.equals(str2)) {
System.out.println("the strings are equal");
}
25
An Implementation of Point
Here's the Point class with both toString and equals overriden.
public class Point {
private int x;
private int y;
public Point(int newX, int newY){
x = newX;
y = newY;
}
public boolean equals(Object o) {
Point other = (Point) o;
return (x == other.x && y == other.y);
}
public String toString(){
return "(" + x + ", " + y + ")";
}
26
Main
27
Lab 1
Modify the previous lab(Inheritance Lecture Lab 1) which contains Student
and GradStudent classes.
The Student class now has an additional private variable double gpa. Modify
the constructor accordingly.
28
Lab 1
29