CH4 Notes - Defining Classes and Methods Chapter 4...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Defining Classes and Methods Chapter 4 Objectives become familiar with the concept of a class and an object that instantiates the class learn how to define classes in Java learn how to define and use methods in Java learn how to create objects in Java learn how parameters work in Java Objectives, cont. learn about information hiding and encapsulation become familiar with the notion of a reference (to understand class variables and class parameters) (optional) learn more about applets Outline Class and Method Definitions Information Hiding and Encapsulation Objects and Reference (optional) Graphics Supplement Class and Method Definitions: Outline Class Files and Separate Compilation Instance Variables Using Methods void Method Definitions Methods that Return a Value Accessing Instance Variables Local Variables Class and Method Definitions: Outline, cont. Blocks Parameters of a Primitive Type Class and Method Definition Syntax Basic Terminology Objects can represent almost anything. A class defines a kind of object. It specifies the kinds of data an object of the class can have. It provides methods specifying the actions an object of the class can take.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
An object satisfying the class definition instantiates the class and is an instance of the class. Basic Terminology, cont. The data items and the methods are referred to as members of the class . We will call the data items associated with an object the instance variables of that object (i.e. that instance of the class). A Class as an Outline A UML Class Diagram Class Files and Separate Compilation Each Java class definition should be in a file by itself. The name of the file should be the same as the name of the class. The file name should end in .java A Java class can be compiled before it is used in a program The compiled byte code is stored in a file with the same name, but ending in .class Class Files and Separate Compilation, cont. If all the classes used in a program are in the same directory as the program file, you do not need to import them. Example class SpeciesFirstTry Using Methods two kinds of methods: methods that return a single value (e.g. nextInt ) methods that perform some action other than returning a single value (e.g println ), called void methods Methods That Return a Value example int next = keyboard.nextInt(); keyboard is the calling object. You can use the method invocation any place that it is valid to use of value of the type returned by the method. Methods That Do Not Return a Value example System.out.println(“Enter data:”);
Background image of page 2
System.out is the calling object. The method invocation is a Java statement that produces the action(s)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

CH4 Notes - Defining Classes and Methods Chapter 4...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online