Knowee
Questions
Features
Study Tools

Program on Polymorphism: Implement a Program to demonstrate method overriding A boy has his money deposited $1000, $1500, and $2000 in banks-Bank A, Bank B and Bank C respectively. We have to print the money deposited by him in a particular bank.Create a class 'Bank' with a method 'getBalance' which returns 0. Make its three subclasses named 'BankA', 'BankB' and 'BankC' with a method with the same name 'getBalance' which returns the amount deposited in that particular bank. Call the method 'getBalance' by the object of each of the three bankswrite a java code and its explaination

Question

Program on Polymorphism: Implement a Program to demonstrate method overriding A boy has his money deposited 1000,1000, 1500, and $2000 in banks-Bank A, Bank B and Bank C respectively. We have to print the money deposited by him in a particular bank.Create a class 'Bank' with a method 'getBalance' which returns 0. Make its three subclasses named 'BankA', 'BankB' and 'BankC' with a method with the same name 'getBalance' which returns the amount deposited in that particular bank. Call the method 'getBalance' by the object of each of the three bankswrite a java code and its explaination

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Java code that demonstrates method overriding and polymorphism:

// Create a Bank class with a method getBalance that returns 0
class Bank {
    public int getBalance() {
        return 0;
    }
}

// Create three subclasses of Bank - BankA, BankB, and BankC
class BankA extends Bank {
    @Override
    public int getBalance() {
        return 1000;
    }
}

class BankB extends Bank {
    @Override
    public int getBalance() {
        return 1500;
    }
}

class BankC extends Bank {
    @Override
    public int getBalance() {
        return 2000;
    }
}

// Main class to test the program
public class Main {
    public static void main(String[] args) {
        // Create objects of each bank
        BankA bankA = new BankA();
        BankB bankB = new BankB();
        BankC bankC = new BankC();

        // Call the getBalance method for each bank
        System.out.println("Balance in Bank A: $" + bankA.getBalance());
        System.out.println("Balance in Bank B: $" + bankB.getBalance());
        System.out.println("Balance in Bank C: $" + bankC.getBalance());
    }
}

Explanation:

  1. We create a Bank class with a method getBalance that returns 0.
  2. We create three subclasses of Bank - BankA, BankB, and BankC.
  3. Each subclass overrides the getBalance method and returns the amount deposited in that particular bank.
  4. In the Main class, we create objects of each bank and call the getBalance method for each bank.
  5. The program then prints the balance in each bank.

Output:

Balance in Bank A: $1000
Balance in Bank B: $1500
Balance in Bank C: $2000

This program demonstrates polymorphism as the getBalance method is overridden in each subclass, and the appropriate implementation is called based on the object type.

This problem has been solved

Similar Questions

Write a java program to perform the concept of method overriding for calculating simple interest of 3 banks A,B,C., Bank A’s rate of interest-10%, Bank’s B rate of interest-9%, Bank’C rate of interest -7%.

Write a C++ program to implement a class called Bank that has private member variables for account_number, operation_type,old_balance , amount, and  new_balance.Include member function “calculate “that prints the balance after deposit or withdrawal.Sample output1011 (account number)D  (D-deposit / W-withdraw)1000 (old balance)2000 (amount)Sample Output3000

Write a java program to create a bank account with Name of the account holder, type of account(savings or  current), Account number and Balance amount in the account. Also, create 5 customers for the class Bank_account using array of objects. Access the customers to perform credit, debit and display of balance.Input 1:Enter the customers:ReenaSavings1231000RobinCurrent4562000PraveenSavings7893000SherinCurrent2342500DeenaSavings5672000Enter choice:1. Credit2. Debit Output :ReenaSavings1232000

Write a C++ program to implement a class called BankAccount that represents a simple bank account. The class should have private member variables for account number and balance. Include member functions to deposit and withdraw money from the account.Testcase:Input:Rs.1000.0  // initial balanceRs.500.0  // depositRs.200.0   // withdrawOutput:Rs.1300.0  // final balance Input:Rs.2000.0  // initial balanceRs.1000.0  // depositRs.4000.0   // withdrawOutput:NA  // final balance

Program that creates a class Account that stores a variable balance. The class has methods to startaccount, to deposit money, to withdraw money and tell the current balance amoun

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.