2Inheritance.pdf-CPS209: Computer Scienc...
2Inheritance.pdf-CPS209: Computer Science II Inheritance Inheritance
Showing 37-43 out of 50
2Inheritance.pdf-CPS209: Computer Science II Inher...
2Inheritance.pdf-CPS209: Computer Science II Inheritance Inheritance
2Inheritance.pdf-CPS209: Computer S...
2Inheritance.pdf-CPS209: Computer Science II Inheritance Inheritance
Page 37
Superclass Construction
Pass parameters to superclass constructor
Must be the
first
statement in subclass constructor
public class Executive extends Employee
{
private double bonus;
private boolean payBonus;
public Executive(String name, double payRate, double bonus)
{
// initialize inherited variables by calling superclass
// constructor method
super(name, payRate);
// initialize new variables
this.bonus = bonus;
payBonus
= false;
}
...
}


Page 38
Converting from subclasses to superclasses
Ok to convert subclass reference to superclass
reference (not other way around though!)
Executive exec
=
new Executive();
Employee
empl
= exec;
Object
o
= exec;
However, Superclass references don't know the
full story:
o.setBonus(1000.0);
// ERROR!
empl.setBonus(1000.0); // ERROR!


Page 39
Subclass HourlyWorker
public class
HourlyWorker
extends Employee
{
private double hoursPerWeek;
public
HourlyWorker(String name, double
payrate, double
hoursPerWeek)
{
super(name,payrate);
this.hoursPerWeek = hoursPerWeek;
}
public double getHoursPerWeek()
{
return hoursPerWeek;
}
public
void
setHoursPerWeek(double hours)
{
hoursPerWeek
= hours;
}
}


Page 40
Polymorphism
Employee
employee;
Executive
exec
= new Executive(“bossman”,150.0);
HourlyWorker
worker = new
HourlyWorker(“joe”,10.0,20);
employee
= exec;
double weeklyPay = employee.computePay();
employee
= worker;
weeklyPay = employee.computePay();
JVM looks at
type of object the reference variable is pointing to (i.e at
run time) and executes the correct method for that object
If reference variable
employee
is pointing to an Executive object,
execute the computePay() method of Executive
If reference variable
employee
is pointing to an HourlyWorker object,
execute the computePay() method of HourlyWorker


Page 41
Polymorphism
Remember: ok to convert subclass reference to superclass reference but
not other way around
Executive
exec
=
new Executive();
Employee
empl
= exec;
If you want to convert superclass ref. variable to subclass, you must cast:
Must make sure superclass ref. variable is referring to a subclass object!!
Executive exec2;
if (empl instanceof Executive)
exec2
= (Executive)employee;


Page 42
Access control level
public
private
protected
(accessible by subclasses and
package)
package access (the default, no modifier)


Page 43
Recommended Access Levels
Fields: Always private
Exception: public static final constants
Methods: public or private
Classes: public or package
Don't use protected
Beware of accidental package access (forgetting
public or private)


Ace your assessments! Get Better Grades
Browse thousands of Study Materials & Solutions from your Favorite Schools
Ryerson University
Ryerson_University
School:
Computer_Science_II
Course:
Great resource for chem class. Had all the past labs and assignments
Leland P.
Santa Clara University
Introducing Study Plan
Using AI Tools to Help you understand and remember your course concepts better and faster than any other resource.
Find the best videos to learn every concept in that course from Youtube and Tiktok without searching.
Save All Relavent Videos & Materials and access anytime and anywhere
Prepare Smart and Guarantee better grades

Students also viewed documents