Inheritance in Java: SavingsAccount as a Subclass of BankAccount, Lecture notes of Java Programming

The concept of inheritance in Java through the example of a SavingsAccount that extends BankAccount. It covers the advantages of inheritance, the relationship between super and subclasses, and the implementation of methods and fields in the subclass. It also discusses encapsulation, method overriding, and constructor calls.

Typology: Lecture notes

2021/2022

Uploaded on 08/01/2022

fioh_ji
fioh_ji 🇰🇼

4.5

(70)

814 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
INHERITANCE
Chapter 10
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Inheritance in Java: SavingsAccount as a Subclass of BankAccount and more Lecture notes Java Programming in PDF only on Docsity!

INHERITANCE

Chapter 10

INHERITANCE

 Mechanism for enhancing existing classes  You need to implement a new class  You have an existing class that represents a more general concept is already available.  New class can inherit from the existing class.  Example  BankAccount  SavingsAccount  Most of the methods of bank account apply to savings account  You need additional methods.  In savings account you only specify new methods.

INHERITANCE

 BankAccount will have the methods  deposit( )  withdraw( )  getBalance( )  SavingsAccount will have the methods  deposit( )  withdraw( )  getBalance( )  addInterest( )

HIERARCHIES

Archosaurs Thecodonts Pterosaurs Dinosaurs Saurischians Ornithischians Crocodilians

RELOOK AT SAVINGSACCOUNT

public class SavingsAccount extends BankAccount { public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } private double interestRate; }

  • Encapsulation: addInterest calls

getBalance rather than updating the

balance field of the superclass (field is

private)

  • Note that addInterest calls

getBalance without specifying an implicit

parameter (the calls apply to the same object) An Introduction to Inheritance

CHECK

 If the class Manager extends the class Employee, which class is the superclass and which is the subclass?

  • Consider a bank that offers its customers the following account types: 1. Checking account: no interest; small number of free transactions per month, additional transactions are charged a small fee 2. Savings account: earns interest that compounds monthly
  • Inheritance hierarchy:
  • All bank accounts support the getBalance method
  • All bank accounts support the deposit and withdraw methods, but the implementations differ
  • Checking account needs a method deductFees; savings account needs a method addInterest

Hierarchy of Bank Accounts

  • Can't override fields
  • Inherit field: All fields from the superclass are automatically inherited
  • Add field: Supply a new field that doesn't exist in the superclass
  • What if you define a new field with the same name as a superclass field? - Each object would have two

instance fields of the same name

  • Fields can hold different values
  • Legal but extremely undesirable Inheriting Instance Fields
  • Consider deposit method of CheckingAccount public void deposit(double amount) { transactionCount++; // now add amount to balance ... }
  • Can't just add amount to balance
  • balance is a private field of the superclass
  • A subclass has no access to private fields of its superclass
  • Subclass must use public interface

Inherited Fields are Private

  • Complete method:

public void deposit(double amount)

transactionCount++;

// Now add amount to balance

super.deposit(amount);

Invoking a Superclass Method

SUBCLASS CONSTRUCTOR

 You write a constructor in the subclass  Call the super class constructor  Use the word super  Must be the first statement of the subclass constructor

  • Ok to convert subclass reference to superclass reference

SavingsAccount collegeFund = new

SavingsAccount(10);

BankAccount anAccount =

collegeFund;

Object anObject = collegeFund;

  • The three object references stored in

collegeFund, anAccount, and

anObject all refer to the same object of type

SavingsAccount

Converting Between Subclass and Superclass

Types

Converting Between Subclass and Superclass

Types