2Inheritance.pdf-CPS209: Computer Scienc...
2Inheritance.pdf-CPS209: Computer Science II Inheritance Inheritance
Showing 20-28 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 20
Inheriting Methods
1) Inherit method:
Don't supply a new implementation of a method that exists in superclass
- just use the method in superclass
as if it was part of subclass!
i.e. Superclass method can be applied to the subclass objects
e.g. getBalance() method


Page 21
Inheriting Methods
2) Add new method:
Supply a new method that doesn't exist in the
superclass
New method can be applied only to subclass objects
See Example SavingsAccount addInterest() method


Page 22
Override a Method
3) Override a method:
Write a different implementation of a method that already exists
in the superclass
Must have same
signature
(same name and same parameter
types)
If a method is applied to an object of the subclass type, the
overriding method is executed (not the superclass method)
See Example: CheckingAccount deposit() method


Page 23
Inheriting Instance Fields (Variables)
Can't (i.e. should never!) override superclass
variables
Creates a “shadow” variable (bad!)
Inherit a field (instance variable): All variables from
the superclass are
automatically
inherited
Add new variable: Supply a new variable that doesn't
exist in the superclass


Page 24
Inheriting Instance Fields (Variables)
What if you define a new field with the same name as
a superclass field (i.e. shadow field)?
Each object would have two instance fields of the same
name
Fields can hold different values
Legal but extremely undesirable!!! Don’t ever do it!!


Page 25
Inheritance: Complete Example
Example: Executive
is a
Employee, HourlyWorker
is a
Employee
class Executive
extends Employee
{
new methods
new instance fields
}
All methods of Employee are automatically inherited
All instance variables of Employee are automatically inherited
Ok to call getName(), getTotalHours(), computePay() etc. from Executive
object
Extended class =
superclass
, extending class =
subclass


Page 26
Complete Example: Employee
Object
Employee
Executive
HourlyWorker


Page 27
Class Employee
public class Employee
{
static public final
int STANDARD_HOURS_PER_WEEK = 35;
public
static
int
numEmployees
= 0;
private String name;
private double payRate;
public Employee()
{
name
= "";
payRate = 0;
}
public Employee (String name, double
payRate)
{
this.name
= name;
this.payRate = payRate;
}


Page 28
Class Employee
public
void
setName(String name)
{
this.name
= name ;
}
public String getName()
{
return
name ;
}
public
void
setPayRate( double
newRate)
{
payRate = newRate ;
}
public double getPayRate()
{
return payRate ;
}


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