400 Object-Based Programming Chapter 8 Fig. 8.7Using overloaded (Web design company)

400 Object-Based Programming Chapter 8 Fig. 8.7Using overloaded constructors to initialize objects of class Time2(part 3 Fig. of 3). 8.8 Using Set and Get Methods Private instance variables can be manipulated only by methods of the class. A typical manipulation might be the adjustment of a customer s bank balance (e.g., a privateinstance variable of a class BankAccount) by a method computeInterest. Classes often provide publicmethods to allow clients of the class to set (i.e., assign values to) or get (i.e., obtain the values of) private instance variables. These methods need not be called set and get, but they often are. As a naming example, a method that sets instance variable interestRate would typically be named setInterestRate and a method that gets the interestRate would typically be called getInterestRate. Get methods are also commonly called accessor methods or query methods. Set methods are also commonly called mutator methods (because they typically change a value). It would seem that providing set and get capabilities is essentially the same as making the instance variables public. This is another subtlety of Java that makes the language so desirable for software engineering. If an instance variable is public, the instance variable can be read or written at will by any method in the program. If an instance variable is private, a public get method certainly seems to allow other methods to read the data at will but the get method controls the formatting and display of the data. A public set method can and most likely will carefully scrutinize attempts to modify the instance variable s value. This ensures that the new value is appropriate for that data item. For example, an attempt to set the day of the month for a date to 37 would be rejected, an attempt to set a person s weight to a negative value would be rejected, and so on. So, Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

Leave a Reply