7.
Implement a Java program to demonstrate the concept of inheritance, method
overriding, and the use of super keyword.
// Parent class
class BankAccount {
String accountType = "General Account";
double balance;
// Parent constructor
BankAccount(double balance) {
[Link] = balance;
[Link]("BankAccount constructor called");
// Parent method
void displayDetails() {
[Link]("Account Type : " + accountType);
[Link]("Balance : " + balance);
void withdraw(double amount) {
[Link]("Withdrawing from BankAccount...");
balance -= amount;
// Child class
class SavingsAccount extends BankAccount {
String accountType = "Savings Account"; // Shadowing parent variable
// Child constructor
SavingsAccount(double balance) {
super(balance); // Call parent constructor
[Link]("SavingsAccount constructor called");
}
// Method overriding
@Override
void withdraw(double amount) {
[Link]("Overridden withdraw() in SavingsAccount");
if (amount > balance) {
[Link]("Insufficient funds");
} else {
[Link](amount); // Call parent version
[Link]("Withdrawal successful from SavingsAccount");
void showAccountTypes() {
[Link]("Child accountType : " + accountType);
[Link]("Parent accountType : " + [Link]);
void displayDetails() {
[Link](); // Call parent method
[Link]("Extra Info : This is a Savings Account");
// Main class
public class InheritanceDemo {
public static void main(String[] args) {
SavingsAccount sa = new SavingsAccount(5000);
[Link]("\n--- Showing Account Types ---");
[Link]();
[Link]("\n--- Displaying Details ---");
[Link]();
[Link]("\n--- Performing Withdrawal ---");
[Link](1200);
OUTPUT
BankAccount constructor called
SavingsAccount constructor called
--- Showing Account Types ---
Child accountType : Savings Account
Parent accountType : General Account
--- Displaying Details ---
Account Type : General Account
Balance : 5000.0
Extra Info : This is a Savings Account
--- Performing Withdrawal ---
Overridden withdraw() in SavingsAccount
Withdrawing from BankAccount...
Withdrawal successful from SavingsAccount