Page 147 - CTS - CSA TP - Volume 2
P. 147
COMPUTER SOFTWARE APPLICATION - CITS
}
}
Output:
Explanation:
• In this program, we have a superclass BankAccount and two subclasses SavingsAccount and
CurrentAccount.
• The BankAccount class has methods for depositing, withdrawing, and displaying balance.
• Both SavingsAccount and CurrentAccount classes extend the BankAccount class and override the
withdraw() method with their own specific implementations to handle withdrawal rules.
• In the BankAccountDemo class, we create instances of SavingsAccount and CurrentAccount.
• When we call the withdraw() method on each object, the overridden version of the method is invoked based
on the actual object type, demonstrating polymorphism and method overriding.
These examples illustrate how method overriding enables subclasses to provide their own specific
implementations of methods inherited from their superclass, allowing for code reuse and flexibility in Java
programs.
132
CITS : IT & ITES - Computer Software Application - Exercise 99