Showing 11-19 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 11
Bank Account - example
public class
BankAccount
{
private long
accountNumber;
private double
balance;
public
BankAccount(
long
accountNumber)
{
this
.accountNumber = accountNumber;
balance = 0;
}
public double
getBalance()
{
return
balance;
}
// continued in the next slide...
Page 12
Bank Account - example
public void
deposit(
double
amount)
{
balance += amount;
}
public void
withdraw(
double
amount)
{
balance
-= amount;
}
public void
transfer(
double
amount, BankAccount targetAccount)
{
this.withdraw(amount);
targetAccount.deposit(amount);
}
}
Page 13
Bank Account – usage example
class BankAccountTester
{
public static void main( String[] args)
{
BankAccount aliceAcc =
new
BankAccount(1398723);
BankAccount bobAcc
=
new
BankAccount(1978394);
aliceAcc.deposit(900);
aliceAcc.transfer(700,bobAcc);
// Alice’s balance = 200 ; Bob’s balance = 700
}
}
Page 14
Inheritance: Simple Example –
Adding a new variable and method
public class
SavingsAccount
extends
BankAccount
{
private double
interestRate;
public
SavingsAccount(long account,
double
rate)
{
super(account);
interestRate = rate;
}
public void
addInterest()
{
double interest = getBalance() * interestRate / 100;
deposit(interest);
}
}
Page 15
Inheritance: Simple Example
Encapsulation
:
addInterest
method calls
getBalance
rather than directly updating the
balance
field of the superclass (balance field is
private
in superclass)
Note also that
addInterest
can call
getBalance
without specifying a “this” implicit
parameter (the calls apply to the same object)
Page 16
Inheritance: Simple Example
class SavingsAccountTester
{
public static void main( String[] args)
{
BankAccount aliceAcc
=
new
BankAccount(1398723);
SavingsAccount sallyAcc =
new
SavingsAccount(1978394,5.5);
sallyAcc.deposit(300);
aliceAcc.deposit(900);
sallyAcc.addInterest();
aliceAcc.transfer(700,sallyAcc);
System.out.println(“Sally Balance = “ + sallyAcc.getBalance());
}
}
Page 17
Inheritance: Simple Example
SavingsAccount
object
automatically
inherits the
balance
and
accountNumber
instance variables from
BankAccount
,
and adds one additional instance variable:
interestRate
:
SavingsAccount
accountNumber
balance
interestRate
Page 18
Self-Check
1.
What instance variables does an object of class
SavingsAccount
have?
2.
Name four methods that you can apply to
SavingsAccount
objects
Page 19
Answers
1.
Three instance fields:
balance, accountNumber
and
interestRat
e.
2.
deposit()
,
withdraw()
,
getBalance()
, and
addInterest()
.
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