3.2 Classes, Objects, Methods and Instance Variables
[Pages:59]1
3.2 Classes, Objects, Methods and Instance Variables
? Class provides one or more methods ? Method represents task in a program
? Describes the mechanisms that actually perform its tasks
? Hides from its user the complex tasks that it performs
? Method call tells method to perform its task
? 2005 Pearson Education, Inc. All rights reserved.
2
3.2 Classes, Objects, Methods and Instance Variables (Cont.)
? Classes contain one or more attributes
? Specified by instance variables ? Carried with the object as it is used
? 2005 Pearson Education, Inc. All rights reserved.
3
3.3 Declaring a Class with a Method and Instantiating an Object of a Class
? Each class declaration that begins with keyword public must be stored in a file that has the same name as the class and ends with the .java filename extension.
? 2005 Pearson Education, Inc. All rights reserved.
4
Class GradeBook
? keyword public is an access modifier ? Class declarations include:
? Access modifier ? Keyword class ? Pair of left and right braces
? 2005 Pearson Education, Inc. All rights reserved.
5
Class GradeBook
? Method declarations
? Keyword public indicates method is available to public
? Keyword void indicates no return type ? Access modifier, return type, name of method and
parentheses comprise method header
? 2005 Pearson Education, Inc. All rights reserved.
6
Common Programming Error 3.1
Declaring more than one public class in the same file is a compilation error.
? 2005 Pearson Education, Inc. All rights reserved.
1 // Fig. 3.1: GradeBook.java 2 // Class declaration with one method.
7
Outline
3
4 public class GradeBook
5 {
GradeBook.java
6
// display a welcome message to the GradeBook user Print line of text to output
7
public void displayMessage()
8
{
9
System.out.println( "Welcome to the Grade Book!" );
10 } // end method displayMessage
11
12 } // end class GradeBook
? 2005 Pearson Education, Inc. All rights reserved.
8
Class GradeBookTest
? Java is extensible
? Programmers can create new classes
? Class instance creation expression
? Keyword new ? Then name of class to create and parentheses
? Calling a method
? Object name, then dot separator (.) ? Then method name and parentheses
? 2005 Pearson Education, Inc. All rights reserved.
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- box variables in python understanding variables and
- python lesson 2 variables operations and types
- python a simple tutorial
- 1 functions in python
- 1 the assignment statement and types
- 3 2 classes objects methods and instance variables
- working with functions in python
- python classes and objects
- python and fme safe software
- a guide to f string formatting in python
Related searches
- java classes objects and methods
- 1 or 3 2 0 5 374 374 168 1 1 default username and password
- 1 or 3 2 0 5 711 711 168 1 1 default username and password
- 1 or 3 2 0 5 693 693 168 1 1 default username and password
- 1 or 3 2 0 5 593 593 or 2dvchrbu 168 1 1 default username and password
- 1 or 3 2 0 5 910 910 168 1 1 default username and password
- 1 or 3 2 0 5 364 364 168 1 1 admin username and password
- 192 1 or 3 2 0 5 33 33 1 1 default username and password
- 1 or 3 2 0 5 633 633 168 1 1 admin username and password
- 192 1 or 3 2 0 5 735 735 1 1 default username and password
- 1 or 3 2 0 5 948 948 168 1 1 admin username and password
- 192 1 or 3 2 0 5 372 372 1 1 default username and password