COMP 14: Writing ClassesAnnouncementsReviewTodayEncapsulationSlide 6Slide 7Slide 8Visibility modifiersSlide 10Slide 11Slide 12Method declarationsSlide 14the return statementParametersHomeworkCOMP 14: Writing ClassesJune 6, 2000Nick VallidisAnnouncementsAssignment P3 due today!Midterm exam is on FridayAssignment P4 goes out today (due next Tuesday, June 13)ReviewWhat'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?TodayEncapsulationMethod DeclarationEncapsulationYou can take two views of an object:internal - the structure of its data, the algorithms used by its methodsexternal - the interaction of the object with other objects in the programFrom the external view, an object is an encapsulated entity, providing servicesThese services define the interface to the objectEncapsulationAn object should be self-governingAny changes to the object's state (its variables) should be accomplished by that object's methodsEncapsulationWe should make it difficult, if not impossible, for one object to "reach in" and alter another object's stateThe 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)EncapsulationAn 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 modifiersIn Java, we accomplish encapsulation through the use of visibility modifiersA modifier is a Java reserved word that specifies particular characteristics of a method or data valueWe've used the modifier final to define a constantVisibility modifiersJava has three visibility modifiers: public, private, and protectedpublic - accessed from anywhereprivate - only from within the classdefault (no modifier) - only from within the packageprotected - we won't talk about thisVisibility modifiersNo object's data should be declared with public visibilityMethods that provide the object's services are declared with public visibility Public methods are also called service methodsVisibility modifiersA method created simply to assist a service method is called a support methodSince a support method is not intended to be called by a client, it should not be declared with public visibilityMethod declarationsA 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 declarationsThe 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 statementThe return type of a method indicates the type of value that the method sends back to the calling locationA method that does not return a value has a void return typeThe return statement specifies the value that will be returnedIts expression must conform to the return typeParametersEach 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;}Homeworkread 4.1-4.6assignment P4 is due next
View Full Document