You are on page 1of 2

Java Assignment 1

1) Write a program where public class HdfcBank inherits another public class GenericBank. Create one more public class Test.java which has the main method which is used for creating reference variables and calling member methods on the instance variables. See if you can declare a reference variable of super class which references to the subclass object (something like GenericBank hb = new HdfcBank()) Also declare one more instance variable as GenericBank hb1 = new GenericBank() 2) Add a method public void welcome (which prints Welcome) in the GenericBank class. Are you able to call the super class method using the hb and hb1 reference variables? What happens at runtime? 3) Add the method public void welcome (prints Welcome to HDFC Bank) in the HdfcBank class. Call the method Welcome using the hb and hb1 instance variables. Have you now got an idea of how runtime polymorphism works? 4) Create one more class SocialBank, try to inherit the HdfcBank class from GenericBank and SocialBank classes. Why that is not allowed. Is it the diamond problem? 5) Add two interfaces WithdrawContract and DepositContract. Are you able to implement these interfaces from HdfcBank class? 6) Add an instance variable bankbalance to the GenericBank class. Will you make that variable private, protected, default or public? 7) Add a method public void deposit (int amount) in the GenericBank class. Call this method using the hb instance variable. 8) Add a method public void withdraw (int amount) in the GenericBank class. Call this method using the hb instance variable. 9) Add a method public void checkbalance (int amount) in the GenericBank class. Call this method using the hb instance variable. 10) Add more versions of these methods as listed below as the amount can be in float numbers also: public void deposit (double amount) public void withdraw (double amount) public void checkbalance (double amount) Is it also a form of polymorphism? What is the name given to such methods?

11) Can you guess the benefit of adding the methods deposit, withdraw and checkbalance? You could also have made the bankbalance instance variable as public and performed various arithmetic operations by accessing that variable from anywhere? Is this data hiding? 12) The Test class simulates the customer of the bank. Can you think of a situation where the customers have to manage the workflow of the bank? Is the use of HdfcBank class for maintaining balance and providing operations on the account provides an abstraction for the customer? (The code in HdfcBank could also have been written in the Testclass itself)

Note: 1. 30/07/12 is the last date for submitting assignment. ( Soft copy is enough)

You might also like