Showing 29-36 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 29
Class Employee
// Compute
and return an employee's **weekly** pay
public double computePay()
{
return payRate * STANDARD_HOURS_PER_WEEK;
}
}
Page 30
Adding Variables and Methods
public class Executive extends Employee
{
private double
bonus;
private boolean payBonus;
public void
setBonus(double bonus)
{
this.bonus
= bonus;
}
public double
getBonus()
{
return bonus;
}
public void
setPayBonus()
{
payBonus
= true;
}
Page 31
Overriding Methods
// class Executive continued...
// Override the
computePay() method
// This version is ok but not as good as version on slide 34
public double
computePay()
{
if (payBonus)
{
double pay =
STANDARD_HOURS_PER_WEEK*getPayRate()+ bonus;
payBonus
= false;
return pay;
}
else
return STANDARD_HOURS_PER_WEEK * getPayRate();
}
Page 32
Encapsulation: inherited fields are private
Consider computePay() method of Executive
Can't just use/access inherited “payRate”
variable directly inside computePay()
payRate is a
private
field of the superclass
Subclass must use public interface of superclass
Can get around this by declaring variables
public
or
protected
in superclass (but don’t!)
Page 33
Overriding Methods
public double computePay()
{
if (payBonus)
{
double pay =
computePay() + bonus; // ERROR!!
payBonus
= false;
return pay;
}
else
return
computePay(); // ERROR!!
}
Let’s look at alternate version of
computePay()
in class Executive:
We want to execute the
computePay()
method of the superclass
Employee
This is common as in our overridden method we just want to add
some extra code then call the normal computePay() method
If we just try to call it as below, we will end up have
computePay()
call itself endlessly
Page 34
Overriding Methods
public double computePay()
{
if (payBonus)
{
double pay =
super.computePay() + bonus;
payBonus
= false;
return pay;
}
else
return super.computePay();
}
What we need to do is to use
super.
Page 35
Invoking a superclass method
Can't just call
computePay()
inside
computePay()
method of class Executive
That is the same as this.computePay()
Calls the same method (infinite recursion!!!)
Instead, invoke
superclass method
super.computePay()
Now correctly calls computePay() method of
superclass Employee
Page 36
Creating subclass objects: example
public class ExecutiveTester
{
public static void main(String[] args)
{
Executive exec = new Executive();
exec.setName(“bossman”);
exec.setPayRate(150.0);
// Calls class Executive computePay
double weeklyPay = exec.computePay();
System.out.println(exec.getName() + “makes “ +
weeklyPay);
}
}
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:
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
lab 18.docx
lab_18.docx
Course
Course
3
Module5QuizSTA2023.d...
Module5QuizSTA2023.docx.docx
Course
Course
10
Week 7 Test Math302....
Week_7_Test_Math302.docx.docx
Course
Course
30
Chapter 1 Assigment ...
Chapter_1_Assigment_Questions.docx.docx
Course
Course
5
Week 4 tests.docx.do...
Week_4_tests.docx.docx
Course
Course
23
Week 6 tests.docx.do...
Week_6_tests.docx.docx
Course
Course
106