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

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

COMP 14 Writing Classes June 6 2000 Nick Vallidis Announcements 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 Declaration Encapsulation 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 object Encapsulation An object should be self governing Any changes to the object s state its variables should be accomplished by that object s methods Encapsulation 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 Methods Client interface methods Data Visibility 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 constant Visibility 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 this Visibility 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 methods Visibility 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 visibility Method declarations A method declaration begins with a method header int add int num1 int num2 method name return type 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 Method declarations The method header is followed by the method body int add int num1 int num2 int sum num1 num2 return sum 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 the 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 type Parameters Each 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 Homework read 4 1 4 6 assignment P4 is due next Tuesday


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 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?