UHCL CSCI 3134 - More on Object Oriented Programming

Unformatted text preview:

Slide 1Slide 2Outline8.2 Case Study: Fig. 8.1 (Time1.java)Slide 5Slide 6The client application Fig. 8.2 (TimeTest.java)Slide 8Slide 9Slide 10Slide 11Slide 128.3 Controlling Access to MembersSlide 14Slide 15Slide 16Slide 178.5 Overloaded ConstructorsSlide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 288.7 Notes on Set and Get MethodsSlide 30Slide 31Slide 32A few words about software maintenanceSlide 34Slide 35Slide 368.8 Composition8.9 EnumerationsSlide 39Slide 40Slide 41Slide 42Slide 438.11 static Class MembersSlide 45Slide 46Slide 478.13 final Instance VariablesSlide 498.14 PackagesSlide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 57Slide 58Slide 59Slide 60Slide 61Slide 62Slide 63More on Object-Oriented Programming1-Based on slides from Deitel & Associates, Inc.- Revised by T. A. Yang23Outline•Review: building classes, controlling access to members of a class, creating constructors•Composition — a capability that allows a class to have references to objects of other classes as members•enum types•static class members•final instance variables•How to organize classes in packages to help manage large applications and promote reuse48.2CaseStudy:Fig.8.1(Time1.java)•Class: Time1 represents the time of day. •private int instance variables: hour, minute and second represent the time in universal-time format (24-hour clock format in which hours are in the range 0–23)•public methods –setTime(), toUniversalString() and toString()–The set of public methods are called the public services or the public interface that the class provides to its clients. •Exercise: Use a class diagram to show the design of Time1.567TheclientapplicationFig.8.2(TimeTest.java)8•Exercise: Use a class diagram to show the design of this application (Time1, TimeTest, and their associations).9•Class Time1 does not declare a constructor, so the class has a default constructor that is supplied by the compiler. •Each instance variable implicitly receives the default value 0 for an int. −Instance variables also can be initialized when they are declared in the class body, using the same initialization syntax as with a local variable. •Public interface−Method setTime (lines 12–25) declares three int parameters and uses them to set the time. −Lines 15–16 test each argument to determine whether the value is in the proper range, and, if so, lines 18–20 assign the values to the hour, minute and second instance variables.10 Method setTime() and Exception Handling•For incorrect values, setTime throws an exception of type IllegalArgumentException (lines 23–24)•Notifies the client code that an invalid argument was passed to the method. •The client can use try...catch to catch exceptions and attempt to recover from them. •The throw statement (line 23) creates a new object of type IllegalArgumentException. In this case, we call the constructor that allows us to specify a custom error message. •After the exception object is created, the throw statement immediately terminates method setTime and the exception is returned to the client code that attempted to set the time.11•The principle of data hiding−The instance variables hour, minute and second are each declared private. −The actual data representation used within the class is of no concern to the class’s clients. −Alternatively, Time1 may represent the time internally as the number of seconds since midnight or the number of minutes and seconds since midnight. −When the actual/internal representations are changed, the clients could continue to use the same public methods/interface and get the same results without being aware of the changes.12138.3ControllingAccesstoMembers•Access modifiers public and private control access to a class’s variables and methods. •public methods present to the class’s clients a view of the services the class provides (the class’s public interface). •Clients need not be concerned with how the class accomplishes its tasks. –For this reason, the class’s private variables and private methods (i.e., its implementation details) are not accessible to its clients. •private class members are not accessible outside the class.•See http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html for complete discussion on access modifiers.148.4ReferringtotheCurrentObject’sMemberswiththethisReference•Every object can access a reference to itself with keyword this.•When a non-static method is called for a particular object, the method’s body implicitly uses keyword this to refer to the object’s instance variables and other methods. –Enables the class’s code to know which object should be manipulated (especially when ambiguity is present). –Can also use keyword this explicitly in a non-static method’s body. •If a method contains a local variable with the same name as a field, that method uses the local variable rather than the field. −The local variable shadows the instance variable in the method’s scope. •A method can use the this reference to refer to the shadowed instance variable explicitly.•Example: Fig. 8.41617188.5OverloadedConstructors•Overloaded constructors enable objects of a class to be initialized in different ways. •To overload constructors, simply provide multiple constructor declarations with different signatures. •The compiler differentiates signatures by the number of parameters, the types of the parameters and the order of the parameter types in each signature.19•Class Time2 (Fig. 8.5) contains five overloaded constructors that provide convenient ways to initialize objects of the new class Time2. •The compiler invokes the appropriate constructor by matching the number, types and order of the types of the arguments specified in the constructor call with the number, types and order of the types of the parameters specified in each constructor declaration.202122•A program can declare a so-called no-argument constructor that is invoked without arguments. e.g., public Time2( )−Such a constructor simply initializes the object as specified in the constructor’s body. •Using this in method-call syntax as the first statement in a constructor’s body invokes another constructor of the same class.–Popular way to reuse initialization code provided by another


View Full Document

UHCL CSCI 3134 - More on Object Oriented Programming

Download More on Object Oriented Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view More on Object Oriented Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view More on Object Oriented Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?