原文地址: http://extreme-java.blogspot.com/2011/02/exercise-for-object-oriented.html
For all those programmers who are new to Java and have read some book but need some Java exercise or Java Tutorial can use this first write up to begin writing Java programs. You can read the steps and accordingly write your code. There will be some steps which I have not mentioned (like the name of your Java project if using a Java IDE). You can either use what you feel comfortable or leave query in the comments.
Basically, I am covering Inheritance, Abstraction, Data Hiding and Polymorphism in this Java OOPS exercise.
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 Test class itself)
For all those programmers who are new to Java and have read some book but need some Java exercise or Java Tutorial can use this first write up to begin writing Java programs. You can read the steps and accordingly write your code. There will be some steps which I have not mentioned (like the name of your Java project if using a Java IDE). You can either use what you feel comfortable or leave query in the comments.
Basically, I am covering Inheritance, Abstraction, Data Hiding and Polymorphism in this Java OOPS exercise.
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 Test class itself)