4 - Lec - Classes and Methods
4 - Lec - Classes and Methods
Basic Terminology
• A class defines a kind of objects:
• specifies the kinds of attributes (data) an object of the class
can have.
• provides methods specifying the actions an object of the class
can take.
• Person class
• Attributes: name, address, phone number
• Methods: change address, change phone number
• Alice object
• Name is Alice, address is …
• Bob object
• Name is Bob, address is …
A Class as
an Outline
Methods
• 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
• Calling a method
• Object name, then dot separator (.)
• Then method name and parentheses
5
Method Definitions
• Methods belong to a class
• Defined inside the class
• Heading
• Return type (e.g. int, float, void)
• Name (e.g. nextInt, println)
• Parameters (e.g. println(…) )
• More…
• Body
• enclosed in braces {}.
• Declarations and/or statements.
7
• GradeBook.java
1 // Fig. 3.1: GradeBook.java
2 // Class declaration with one method.
3
4 public class GradeBook
5 {
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
8
• GradeBookTest.java
1 // Fig. 3.2: GradeBookTest.java
2 // Create a GradeBook object and call its displayMessage method.
3
4 public class GradeBookTest
5 {
6 // main method begins program execution
7 public static void main( String args[] )
8 {
9 // create a GradeBook object and assign it to myGradeBook Use class instance creation
10 GradeBook myGradeBook = new GradeBook(); expression to create object of
11 class GradeBook
12 // call myGradeBook's displayMessage method
13 myGradeBook.displayMessage();
Call method
displayMessage using
14 } // end main
GradeBook object
15
16 } // end class GradeBookTest
1. Return a value
• next = keyboard.nextInt();
• keyboard is the calling object.
…
return x;
}
Example of Scope
public void method()
{
for (int i=0; i < 10; i++) // start of a block
{
int y=0;
…
}
// Are i and y accessible here?
}
Passing Values to a Method:
Parameters
• Input values for methods (within the program, not from user)
• passed values or parameters
• More flexibility for methods
• formal parameters
• Part of method definition
• After the method name, within parentheses
• type
• name
• arguments, or actual parameters
• Calling a method with an object within the parentheses
• matching data type
• in the same order
Formal vs Actual Parameters
public static void main(String[] args)
{
print(“George Bush”); //“George Bush” as argument
}
• Method
• any class can invoke
private
• Attribute (instance variable)
• only the same class can access/change
• Method
• only the same class can invoke
private or public ?
• Attributes (instance variables)
• should be private, why?
• Methods
• usually public, why?
• sometimes private
• The Person class should have a setter method with public access for each attribute.
• The Person class should have a getter method with public access for each attribute.
• For a Person with the following attributes:
title = "Mr."
first name = "William"
last name = "Gates"
age = 44
gender = true (true is male, false is female, or vice versa)
• The Person class should have the following public access methods that return Strings as follows:
standardName() formalName()
concatenation of the first and last names concatenation of the title, first name, and lastname
(i.e., "William Gates") (i.e., "Mr. William Gates")