Unformatted text preview:

COMP 14: Writing ClassesAnnouncementsReviewTodayEncapsulationSlide 6Slide 7Slide 8Visibility modifiersSlide 10Slide 11Slide 12Method declarationsSlide 14the return statementParametersHomeworkCOMP 14: Writing ClassesJune 6, 2000Nick VallidisAnnouncementsAssignment P3 due today!Midterm exam is on FridayAssignment P4 goes out today (due next Tuesday, June 13)ReviewWhat's the MAJOR difference between a class and an object?What two types of things go into a class?What does the scope of data mean?TodayEncapsulationMethod DeclarationEncapsulationYou can take two views of an object:internal - the structure of its data, the algorithms used by its methodsexternal - the interaction of the object with other objects in the programFrom the external view, an object is an encapsulated entity, providing servicesThese services define the interface to the objectEncapsulationAn object should be self-governingAny changes to the object's state (its variables) should be accomplished by that object's methodsEncapsulationWe should make it difficult, if not impossible, for one object to "reach in" and alter another object's stateThe user, or client, of an object can request its services, but it should not have to be aware of how those services are accomplished (abstraction - hide details)EncapsulationAn encapsulated object can be thought of as a black box or an abstraction (p. 54)Its inner workings are hidden to the client, which only invokes the interface methodsClientClientMethodsDataVisibility modifiersIn Java, we accomplish encapsulation through the use of visibility modifiersA modifier is a Java reserved word that specifies particular characteristics of a method or data valueWe've used the modifier final to define a constantVisibility modifiersJava has three visibility modifiers: public, private, and protectedpublic - accessed from anywhereprivate - only from within the classdefault (no modifier) - only from within the packageprotected - we won't talk about thisVisibility modifiersNo object's data should be declared with public visibilityMethods that provide the object's services are declared with public visibility Public methods are also called service methodsVisibility modifiersA method created simply to assist a service method is called a support methodSince a support method is not intended to be called by a client, it should not be declared with public visibilityMethod declarationsA method declaration begins with a method headermethodmethodnamenamereturnreturntypetypeparameter listparameter listThe parameter list specifies the typeThe parameter list specifies the typeand name of each parameterand name of each parameterThe name of a parameter in the methodThe name of a parameter in the methoddeclaration is called a declaration is called a formal argumentformal argumentint add (int num1, int num2)int add (int num1, int num2){int sum = num1 + num2;return sum;}Method declarationsThe method header is followed by the method bodyThe return expression must beThe return expression must beconsistent with the return typeconsistent with the return typesum issum is local datalocal dataIt is created each time the It is created each time the method is called, and is method is called, and is destroyed when it destroyed when it finishes executingfinishes executingthe return statementThe return type of a method indicates the type of value that the method sends back to the calling locationA method that does not return a value has a void return typeThe return statement specifies the value that will be returnedIts expression must conform to the return typeParametersEach time a method is called, the actual arguments in the invocation are copied into the formal argumentsanswer = obj.add (25, count);int add (int num1, int num2){int sum = num1 + num2;return sum;}Homeworkread 4.1-4.6assignment P4 is due next


View Full Document

UNC-Chapel Hill COMP 14 - COMP 14- Writing Classes

Download COMP 14- Writing Classes
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 COMP 14- Writing Classes 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 COMP 14- Writing Classes 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?