Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.

Similar presentations


Presentation on theme: "Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects."— Presentation transcript:

1 Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects

2 In this chapter, you will:  Create methods with no arguments, a single argument, and multiple arguments  Create method that return values  Learn about class concepts  Create a class  Use instance methods  Declare objects  Organize classes  Use constructors

3 Methods  A method is a series of statements that carry out a task  Any class can contain an unlimited number of methods

4 Methods Methods must include:  A declaration  An opening curly brace  A body  A closing brace

5 Methods Method declarations contain:  Optional access modifiers  The return type for the method  The method name  An opening parenthesis  An optional list of method arguments  A closing parenthesis

6 Access Modifiers Access modifiers for a method can be:  public- most often methods are given public access  private – can’t be used by all classes  protected – can’t be used by all classes  static – objects aren’t required in order for these methods to be used

7 Creating Methods that Require a Single Argument  Arguments- Are communications to a method (parameters)  Implementation hiding- Allows that the invoking program must know the name of the method and what type of information to send it, but the calling program does not need to know how the method works

8 Creating Methods that Require a Single Argument The method declaration must include:  The type of the argument  A local name for the argument  For example: public void predictRaise (double moneyAmount)

9 Calling a Method  Can use either argument when calling the method & sending the parameter:  Constant  Variable  predictRaise(472.55)  predictRaise(mySalary)

10 Creating Methods that Require Multiple Arguments  Methods can require more than one argument  Pass multiple arguments by:  Listing the arguments in the call to the method  Separating them with commas  The declaration for a method that receives two or more arguments must list the type for each argument separately public void predictRaise(double moneyAmount, float balance)

11 A Method’s Return Type  The return type is known as the method’s type For example: public static void nameAndAddress()  This method is public and void means it returns no value

12 Return Statement  The return statement is the last statement in a method return balance;  Usually you use the returned value, but this is not required  The return statement sends back a value to the calling program

13 Learning about Class Concepts In object-oriented programming:  Everything can be considered an object  An object is an instantiation of a class, or a tangible example of a class  Every object is a member of a class  Your desk is an object and is a member of the Desk class  These statements represent is-a relationships

14 Learning about Class Concepts The concept of a class is useful because: Objects inherit attributes from classesObjects inherit attributes from classes All objects have predictable attributes because they are members of certain classesAll objects have predictable attributes because they are members of certain classes You must: Create the classes of objects from which objects will be instantiated (Car class)Create the classes of objects from which objects will be instantiated (Car class) Write other classes to use the objects (a program/class is written to drive to the airport & uses the car class to create a car object to drive)Write other classes to use the objects (a program/class is written to drive to the airport & uses the car class to create a car object to drive)

15 Creating a Class You must:  Assign a name to the class  Determine what data and methods will be part of the class To begin, create a class header with three parts:  An optional access modifier  The keyword class  Any legal identifier you choose for the name of the class

16 Access modifiers for Classes Access modifiers include:  public  This is the most used modifier  Most liberal form of access  Can be extended or used as the basis for other classes  final- used only under special circumstances  abstract- used only under special circumstances

17 Field/Variable Modifiers  Private  Public  Static  Final

18 Field Modifiers  Private  No other classes can access a field’s values  Only methods of the same class are allowed to set, get, or otherwise use private variables  Highest level of security  Also called information hiding  Provides a means to control outside access to your data

19 Using Instance Methods  Methods used with object instantiations are called instance methods  You can call class methods without creating an instance of the class (these methods have static as a modifier)  Instance methods require an instantiated object.

20 Declaring Objects To declare an object:  Supply a type and an identifier  Allocate computer memory for the object  Use the new operator Car myCar = new Car( ); Or Car myCar = new Car(blue, V8); The first example uses the defaults as the attribute values & the second sets the color & engine attributes

21 Organizing Classes  Most programmers place data fields in some logical order at the beginning of a class  For example, use a unique identifier for each employee  empNum  Last names and first names are organized together

22 Using Constructor Methods  Constructor methods- Methods that establish an object Employee chauffer = new Employee();  Calls a method named Employee()  When classes are written for the purpose of creating objects, they do not have a main method. Instead, they have a constructor method that sets the default attribute values.

23 Attribute Default Values  Numeric == 0  Character == Unicode ‘\u0000’  Boolean == false  Object types == null


Download ppt "Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects."

Similar presentations


Ads by Google