Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 14: Writing Classes June 6, 2000 Nick Vallidis.

Similar presentations


Presentation on theme: "COMP 14: Writing Classes June 6, 2000 Nick Vallidis."— Presentation transcript:

1 COMP 14: Writing Classes June 6, 2000 Nick Vallidis

2 Announcements zAssignment P3 due today! zMidterm exam is on Friday zAssignment P4 goes out today (due next Tuesday, June 13)

3 Review zWhat's the MAJOR difference between a class and an object? zWhat two types of things go into a class? zWhat does the scope of data mean?

4 Today zEncapsulation zMethod Declaration

5 Encapsulation zYou can take two views of an object: yinternal - the structure of its data, the algorithms used by its methods yexternal - the interaction of the object with other objects in the program zFrom the external view, an object is an encapsulated entity, providing services zThese services define the interface to the object

6 Encapsulation zAn object should be self-governing zAny changes to the object's state (its variables) should be accomplished by that object's methods

7 Encapsulation zWe should make it difficult, if not impossible, for one object to "reach in" and alter another object's state zThe 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)

8 Encapsulation zAn encapsulated object can be thought of as a black box or an abstraction (p. 54) zIts inner workings are hidden to the client, which only invokes the interface methods Client Methods Data

9 Visibility modifiers zIn Java, we accomplish encapsulation through the use of visibility modifiers zA 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 constant

10 Visibility modifiers  Java has three visibility modifiers: public, private, and protected  public - accessed from anywhere  private - only from within the class zdefault (no modifier) - only from within the package  protected - we won't talk about this

11 Visibility modifiers  No object's data should be declared with public visibility  Methods that provide the object's services are declared with public visibility zPublic methods are also called service methods

12 Visibility modifiers zA method created simply to assist a service method is called a support method zSince a support method is not intended to be called by a client, it should not be declared with public visibility

13 Method declarations zA method declaration begins with a method header methodname returntype parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal argument int add (int num1, int num2)

14 { int sum = num1 + num2; return sum; } Method declarations zThe method header is followed by the method body The return expression must be consistent with the return type sum is local data It is created each time the method is called, and is destroyed when it finishes executing

15 the return statement zThe 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 zThe return statement specifies the value that will be returned zIts expression must conform to the return type

16 Parameters zEach time a method is called, the actual arguments in the invocation are copied into the formal arguments answer = obj.add (25, count); int add (int num1, int num2) { int sum = num1 + num2; return sum; }

17 Homework zread 4.1-4.6 zassignment P4 is due next Tuesday!


Download ppt "COMP 14: Writing Classes June 6, 2000 Nick Vallidis."

Similar presentations


Ads by Google