原文地址: 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)
分享到:
相关推荐
A Guide to MATLAB Object-Oriented Programming is the first book to deliver broad coverage of the documented and undocumented object-oriented features of MATLAB®. Unlike the typical approach of other ...
world applications. The book teaches developing web applications using advanced PHP techniques and advanced database concepts, and this edition offers several chapters devoted to object-oriented ...
Starting with a detailed analysis of object-oriented analysis and design, you will use the Python programming language to clearly grasp key concepts from the object-oriented paradigm. This book fully ...
It has been my experience as a .NET trainer and lead programmer that most people do not have trouble ...required to master the higher-level concepts of object-oriented programming methodology and design.
The newcomer will discover a fast track to understanding the concepts of object-oriented programming. At the same time, experienced programmers seeking to strengthen their object-oriented development...
A practical, problem-solving approach to the fundamental concepts of Object Oriented Design and their application using C++. This book is written for the "engineer in the trenches". It is a serious ...
matlab面向对象编程教程-A Guide to MATLAB Object-Oriented Programming - Register, CRC 2007.rar 最近想好好学学matlab面向对象编程的一些东西(以前也自学过,但现在想系统学一下,libsvm工具箱几乎已经没有...
C++ For Artists: The Art, Philosophy, and Science of Object-Oriented Programming by Rick Miller ISBN:1932504028 Biblio Distribution ? 2003 (590 pages) Intended as both a classroom and reference ...
world applications. The book teaches developing web applications using advanced PHP techniques and advanced database concepts, and this edition offers several chapters devoted to object-oriented ...
Implement Object-Oriented concepts in a wide range of frontend architectures Capture objects from real-world elements and create object-oriented code that represents them Learn the latest ES6 features...
Object-Oriented Analysis and Design for Information Systems clearly explains real object-oriented programming in practice. Expert author Raul Sidnei Wazlawick explains concepts such as object ...
面向对象编程(Object-Oriented Programming,简称OOP)是一种软件开发方法论,它以“对象”作为基本单位来构建程序,并通过对象之间的交互来实现功能。本课程旨在帮助学员掌握面向对象的基本概念及其在软件开发中的...
Protocol-oriented programming and first class value semantics have now become two incredibly powerful concepts at the heart of Swift’s design. This book will help you understand the difference ...
If you are an iOS developer who has a basic idea of object-oriented programming and want to incorporate its concepts with Swift to optimize your application's code and create reusable and easily to ...
5.4 OBJECT-ORIENTED SOFTWARE CONSTRUCTION 116 5.5 ISSUES 117 5.6 KEY CONCEPTS INTRODUCED IN THIS CHAPTER 119 5.7 BIBLIOGRAPHICAL NOTES 119 Chapter 6: Abstract data types 121 6.1 CRITERIA 122 6.2 ...
Essential concepts of programming language design and implementation are explained and illustrated in the context of the object-oriented programming language (OOPL) paradigm. Written with the upper-...