0% found this document useful (0 votes)
32 views3 pages

Java Inheritance and Method Overriding

The document provides a Java program that illustrates inheritance, method overriding, and the use of the super keyword through a BankAccount parent class and a SavingsAccount child class. It demonstrates how the child class can override methods and access parent class methods and variables. The main method creates a SavingsAccount instance and showcases account types, details, and a withdrawal operation.

Uploaded by

rrsmanju088
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Java Inheritance and Method Overriding

The document provides a Java program that illustrates inheritance, method overriding, and the use of the super keyword through a BankAccount parent class and a SavingsAccount child class. It demonstrates how the child class can override methods and access parent class methods and variables. The main method creates a SavingsAccount instance and showcases account types, details, and a withdrawal operation.

Uploaded by

rrsmanju088
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

You might also like