Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.

Similar presentations


Presentation on theme: "Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class."— Presentation transcript:

1 Defining Classes I Part B

2 Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class from the implementation details grouping software into a unit in such a way that it is easy to use because there is a well-defined simple interface grouping software into a unit in such a way that it is easy to use because there is a well-defined simple interface data and actions are combined into a single item (class object) and the details of the implementation are hidden data and actions are combined into a single item (class object) and the details of the implementation are hidden

3 API Application Programming Interface Application Programming Interface description of how to use the class description of how to use the class

4 ADT Abstract Data Type Abstract Data Type data type that is written using good information-hiding techniques data type that is written using good information-hiding techniques

5 Access modifiers public = no access restrictions at all public = no access restrictions at all protected (to be discussed in the future) protected (to be discussed in the future) private = the instance variable or method cannot be accessed outside of the class definition private = the instance variable or method cannot be accessed outside of the class definition

6 Access modifiers private = the instance variable or method cannot be accessed outside of the class definition public class Date { private StringmMonth; private intmDay; private intmYear; public boolean equals ( Date other ) { return (mMonth.equalsIgnoreCase( other.mMonth ) &&mDay == other.mDay &&mYear == other.mYear); }…} Is this allowed?

7 Access modifiers private = the instance variable or method cannot be accessed outside of the class definition (but one instance can access private members of another instance) public class Date { private StringmMonth; private intmDay; private intmYear; public boolean equals ( Date other ) { return (mMonth.equalsIgnoreCase( other.mMonth ) &&mDay == other.mDay &&mYear == other.mYear); }…} Is this allowed?Yes!

8 Types of methods 1. Accessor – method that allows one to obtain (a copy of) the (often private or protected) data in a class 2. Mutator – method that allows one to change the data (often private or protected) in a class

9 Preconditions and postconditions Precondition = states what is assumed to be true when a method is invoked Precondition = states what is assumed to be true when a method is invoked assert(ion) can sometimes be used to check assert(ion) can sometimes be used to check Other times we can only state preconditions in comments. Other times we can only state preconditions in comments.

10 Preconditions and postconditions Precondition = states what is assumed to be true when a method is invoked Precondition = states what is assumed to be true when a method is invoked assert(ion) can sometimes be used to check assert(ion) can sometimes be used to check assert boolean_expression; assert boolean_expression; Example: Example: public getData ( int which ) { assert (which>=0); …} Chapter 3, p. 151 Chapter 3, p. 151

11 Preconditions and postconditions Precondition = states what is assumed to be true when a method is invoked Precondition = states what is assumed to be true when a method is invoked Other times we can only state preconditions in comments. Other times we can only state preconditions in comments./** * precondition: all instance variables have values. * precondition: all instance variables have values. * @return a string describing the data in the calling object * @return a string describing the data in the calling object */ */ public String toString ( ) { …}

12 Preconditions and postconditions Postcondition Postcondition Describes the effect of the method call Describes the effect of the method call States what will be true after the method is invoked (assuming that the preconditions have been met) States what will be true after the method is invoked (assuming that the preconditions have been met)

13 Overloading Simply methods w/ the same name but different parameters. Simply methods w/ the same name but different parameters. Rules: Rules: 1. Same name for each 2. Different number of parameters and/or parameter types. 3. All must have the exact same return type. Note: Java only allows methods to be overloaded. (Some languages allow operators to be overloaded as well.) Note: Java only allows methods to be overloaded. (Some languages allow operators to be overloaded as well.)

14 Overloading Example: Example: public void setDate ( int monthInt, int day, int year ) { …} public void setDate ( String monthString, int day, int year ) { …} public void setDate ( int year ) { …}

15 Overloading Good or bad? Good or bad? public double abs ( double x ) { if (x<0)return -x; return x; } public int abs ( int x ) { if (x<0)return -x; return x; }

16 Constructors (ctors) Rules: Rules: 1. Method w/ same name as class name. 2. May have more than one ctor. w/ 0 or more different arguments w/ 0 or more different arguments You get classname() by default. You get classname() by default. 3. No return type may be specified (including void). 4. Invoked via new.

17 Constructor examples from Date class /** no argument constructor */ public Date ( ) { mMonth = "January"; mMonth = "January"; mDay = 1; mDay = 1; mYear = 1000; mYear = 1000;}

18 Constructor examples from Date class /** other constructors */ public Date ( int monthInt, int day, int year ) { // call setDate(..) method to // call setDate(..) method to // initialize the instance variables // initialize the instance variables setDate( monthInt, day, year ); setDate( monthInt, day, year );}

19 Constructor examples from Date class public Date ( String monthString, int day, int year ) { setDate( monthString, day, year ); setDate( monthString, day, year );}

20 Constructor examples from Date class public Date ( int year ) { setDate( 1, 1, year ); setDate( 1, 1, year );}

21 Constructor examples from Date class /** copy constructor: creates an independent * copy of the argument object. * copy of the argument object. */ */ public Date ( Date aDate ) { if (aDate == null) { //Not a real date. if (aDate == null) { //Not a real date. System.out.println("Fatal Error."); System.out.println("Fatal Error."); System.exit(0); System.exit(0); } /* make copies of all instance variables */ /* make copies of all instance variables */ mMonth = aDate.mMonth; mMonth = aDate.mMonth; mDay = aDate.mDay; mDay = aDate.mDay; mYear = aDate.mYear; mYear = aDate.mYear;}

22 Using ctors Date date1 = new Date( "Dec", 16, 1770 ), Date date1 = new Date( "Dec", 16, 1770 ), date2 = new Date( 1, 27, 1756 ), date2 = new Date( 1, 27, 1756 ), date3 = new Date( 1882 ), date3 = new Date( 1882 ), date4 = new Date( ), date4 = new Date( ), date5 = new Date( date3 ); date5 = new Date( date3 ); Which one is the copy ctor?


Download ppt "Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class."

Similar presentations


Ads by Google