




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















Chapter 10
๏ข 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.
๏ข BankAccount will have the methods ๏ deposit( ) ๏ withdraw( ) ๏ getBalance( ) ๏ข SavingsAccount will have the methods ๏ deposit( ) ๏ withdraw( ) ๏ getBalance( ) ๏ addInterest( )
Archosaurs Thecodonts Pterosaurs Dinosaurs Saurischians Ornithischians Crocodilians
public class SavingsAccount extends BankAccount { public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } private double interestRate; }
parameter (the calls apply to the same object) An Introduction to Inheritance
๏ข If the class Manager extends the class Employee, which class is the superclass and which is the subclass?
๏ข 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