Public and Private variables and methods: Access methods It is often the case that some of the variables and methods defined for a class of objects are solely for an object to help perform its tasks. For example a variable to store the intermediate result of some calculation, or a method that performs some calculation on the objects variables. Such variables and methods are not intended to be interrogated or invoked by other objects. For example, consider an object that is an instance of a Loan class. An attribute identified in modeling might be "remaining Loan". When a payment has been made, the payment is deducted from the remaining loan balance and the balance reduced. When interest is calculated it is calculated according to the remaining balance and the added to the balance. However, when a customer wishes to pay off a loan in total, earlier than the original loan period, the loan total to repay is actually calculated with a charge for some portion of the remaining interest. Thus, for such a Loan object, we would not wish other objects to be able to interrogate the variable "remainingLoan" directly, but to be required to interrogate the value of this variable via a "get" method. Thus an method to be implemented might be "getRemainingLoan( purpose )" -- where the argument "purpose" might be one of "installmentPayment", "interestCalculation", "earlyRepayment". The way such a method might be implemented, expressed in an informal way might be: IF (purpose is "installmentPayment" or "interestCalculation") return remainingLoan ELSE return remainingLoan + earlyPaymentCharge(remainingLoan, remainingPeriod) Informal design notations The above statements are not correct Java statements -- they are an informal way for a human to express a design for a part of a program. This kind on structured, informal, English-like notation is called "Pseudocode", and can be very useful for use in the informal design of computer programs. Once a design written informally appears to be right, it can then be coded into correct Java statements and compiled and tested. For situations such as the above, Java provides a mechanism for hiding variables from other objects, thus requiring the use of "get" and "set" methods. Notice that in the above method implementation design we assume there exists some method that will calculate the early repayment charge given the remaining loan balance and the period remaining for the loan. Such a method might be one an analyst decides should not be made available to other objects. Once again, Java provides a method for hiding such internal methods from being invoked by other objects. Variables and methods not available to other objects are known as private. Those that are available to other objects are known as public. The details of how and when to implement public and private methods and variables are introduced in a later unit. Back to top 
RITSEC - Global Campus Copyright ?1999 RITSEC- Middlesex University. All rights reserved. webmaster@globalcampus.com.eg |