




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 28
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