【翻译】Exercise for Object-Oriented Programming Concepts

原文地址: 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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值