CMSC 202 Classes and Objects 2 nd Lecture
CMSC 202 Classes and Objects 2 nd Lecture February 2008
Topics • OOP techniques – encapsulation – data hiding • Visibility of instance variables and methods – public – private • Accessor and mutator methods • Preconditions and postconditions • Method overloading February 2008 2
OOP Techniques • Encapsulation – Combines data and operations into a single entity (a class) • Abstraction – Discarding details • Information hiding – A form of abstraction – Separating the description of how to use a class from the details of the class’ implementation February 2008 3
The Value of Information Hiding With information hiding: • programmers that use a class do not need to know how the class is implemented, only how to use it, • the information the programmer needs to use the class is kept to a minimum, and • class implementation may be changed with no impact on those who use the class. February 2008 4
Date 2 Class In this new date class, the instance variables have been labeled private. public class Date 2 { private String month; private int day; private int year; public void print( ) { System. out. println(month + “ “ + day + “ “ + year); } A method may use the class’ private instance variables // set. Date and month. String included } February 2008 5
Visibility Modifiers Date 1 class - public instance variables were used Date 2 class - private instance variables are now used public class Date 2 Demo { public static void main( String[ ] args ) { Date 2 my. Date = new Date 2( ); my. Date. month = “July”; my. Date. day = 4; my. Date. year = 1950; // compiler error my. Date. set. Date( 7, 4, 1950 ); // OK – why? my. Date. print( ); // OK – why? } } February 2008 6
Private Instance Variables • Good programming practice: – Label all instance variables as private. – The class has complete control over how/when/if the instance variables are changed. • How are private instance variables accessed or modified? – Accessor and mutator methods • give the class user indirect access to the instance variables • degree of access is still controlled by the class implementer February 2008 7
Accessors & Mutator • Accessor method – retrieves the value of a private instance variable – conventional to start the name with get • Mutator method – changes the value of a private instance variable – conventional to start the name of an with set February 2008 8
More Accessors and Mutators Question: Don’t the use of accessors and mutators defeat the purpose of making the instance variable private? Answer: No. • The class implementer decides which instance variables will have accessors. • Mutators can: – validate the new value of the instance variable, and – decide whether or not to actually make the requested change. February 2008 9
Encapsulation February 2008 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 10
Date 2 Accessor and Mutator public class Date 2 { private String month; private int day; // 1 - 31 private int year; // 4 -digit year // accessors return the value of private data public int get. Day ( ) { return day; } // mutators can validate the new value public boolean set. Year( int new. Year ) { if ( new. Year < 1000 || new. Year > 9999 ) { // this is an invalid year return false; } else { year = new. Year; return true; } // rest of class definition follows } February 2008 11
Other Methods • Classes can provide many methods besides accessors and mutators. • Clear communication with the class user is of paramount importance so that he can – use the appropriate method, – and use class methods properly. • Method comments: – explain what the method does, and – describe how to use the method. • Two important types of method comments: – precondition comments – post-conditions comments February 2008 12
Preconditions and Postconditions • Precondition – What is assumed to be true when a method is called – If any pre-condition is not met, the method cannot correctly perform its function. • Postcondition – Describes the effect of the method – States what will be true after the method executes (assuming all pre-conditions are met) February 2008 13
A Simple Example Recall the print method from Date 2 that outputs the month string, day, and year to the screen. The print method might look something like this: /* Pre. Condition: none Post. Condition: The calling object has been written to the screen in the format <month string> <day>, <year> */ public void print( ) { // code here } February 2008 14
Another Common Example Very often the precondition specifies the limits of the parameters and the postcondition says something about the return value. /* Pre-condition: 1 <= month <= 12 day appropriate for the month 1000 <= year <= 9999 Post-condition: The month, day, and year of the calling object have been set to the parameter values. Returns true if the calling object has been changed Returns false otherwise */ public boolean set. Date( int month, int day, int year) { // code here } February 2008 15
What’s in a name? • Many different classes can define a method with the same name (e. g. print). • Java can determine which method to call based on the type of the calling object. • Consider this code snippet: Date 2 birthday = new Date 2(); Dog fido = new Dog( ); birthday. print( ); fido. print( ); – birthday. print( ) will call the print( ) method defined in the Date 2 class. – fido. print( ) will call the print( ) method defined in the Dog class. Jan 23, 2008 16
to. String • One method which should be implemented for all classes is to. String. public String to. String( ) • The to. String method returns a String chosen by the class implementer. • The string typically contains the values of important instance variables in a readable format. • This method is very often used for outputting the “contents” of a class. Jan 23, 2008 17
Method Overloading • Two or more methods in the same class may have the same name. • This technique is known as method overloading. February 2008 18
Overloaded set. Date • The Date 2 class set. Date method: public boolean set. Date( int month, int day, int year ) • But suppose we wanted to change only the day and year? – We can define another method named set. Date like this: public boolean set. Date( int day, int year ) (after all, set. Date is a good descriptive name for what this method does) February 2008 19
Date 3 Class with Overloaded set. Date Method public class Date 3 { private String month; private int day; private int year; // 1 - 31 // 4 digits public boolean set. Date( int new. Month, int new. Day, int new. Year ) { // code here } public boolean set. Date( int new. Day, int new. Year ); { // code here, doesn’t change month } // print(), month. String( ), set. Year( ), etc. follow } February 2008 20
Date 3 Demo Class public class Date 3 Demo { public static void main (String[ ] args) { Date 3 my. Date = new Date 3( ); my. Date. set. Date( 1, 23, 1982 ); my. Date. print( ); my. Date. set. Date( 4, 1999 ); my. Date. print( ); } } How does Java know which set. Date method to call? February 2008 21
Method Signature • In Java (and other OO languages), a method is uniquely identified by – its name and – its parameter list (parameter types and their order). • This is known as its signature. Examples: public boolean February 2008 set. Date(int new. Month, int new. Day, int new. Year) set. Date(String new. Month, int new. Day, int new. Year) set. Date(int new. Day, String new. Month) 22
Return Type is Not Enough • Suppose we attempt to overload Date 3’s print( ) method by using different return types. public void print( ) public boolean print( ) { /* code here */ } • This is NOT valid method overloading because the code that calls print( ) can ignore the return value birthday. print( ); • The compiler can’t tell which print( ) method to call. Just because a method returns a value doesn’t mean the caller has to use it. February 2008 23
Too Much of a Good Thing Automatic type promotion and overloading can sometimes interact in ways that confuse the compiler. Example: public class X {. . . public void print. Average ( int a, double b) //version 1. . . public void print. Average ( double a, int b) //version 2 } And then consider this: my. X = new X( ); my. X. print. Average( 5, 7 ); The Java compiler can’t decide whether to promote 7 to 7. 0 and call the first version of print. Average or to promote 5 to 5. 0 and call the second. The Java compiler is confused and will issue an error stating that the method invocation of my. X. print. Average is ambiguous. February 2008 24
Private Methods • • Methods may be private Cannot be invoked by the class user Can only be called by other class methods Most commonly used as “helper” methods to support top-down implementation of a public method February 2008 25
- Slides: 25