click below
click below
Normal Size Small Size show me how
Chapter 3
Using Methods, Classes, and Objects
Term | Definition |
---|---|
Method | A program module that contains a series of statements that carry out a task. |
Method Calls | To execute a method, you invoke or call it. In other words, a calling method makes a method call, and the method call invokes a called method. The calling method is also known as a client method because a called method provides a service for its client. |
Abstraction | Using a method name to contain or encapsulate a series of statement |
Understanding Method Construction | Every method must include two parts... 1. A method header.. 2. A method body |
Method Header | Provides information about how other methods can interact with it. A method header is also called a declaration. Is the first line of a method and contains... 1. Optional access specifiers.. 2. A return.. 3. An identifier.. 4. Parentheses |
Method Body | A method body between a pair of curly braces - The method body contains the statements that carry out the work of the method. A method's body is called its implementation. Technically, a method is not required to contain any statements in its body. |
Stub | An empty method. Sometimes, while developing a program, the programmer creates an empty method as a placeholder and fills in the implementation later. |
Access Specifiers(Access Modifiers) | The access specifier for a Java method can be any of the following modifiers: public, private, protected, or, if left unspecified, package by default |
Public Access Specifier | Endowing a method with public access means that any other class can use it, not just the class in which the method resides. Most often, methods are given public access |
Static Modifier | The static modifier means that these methods do not require an object to be created |
Return Type | Describes the type of data the method sends back to its calling method. Not all method return a value to their calling methods; a method that returns no data has a return type of void |
Void | The phrases void method and method of type void both refer to a method that has a void return type |
main() Method | Must specify public access, must use the keyword static, must have a void return type, must be named main(), and must contain String[] and an identifier(args is traditional) within its parentheses. Ex: public static void main(String[] args) |
Fully Qualified Identifier | A complete name that includes the class. Within a class a method name alone is sufficient, but outside the class you need to use the fully qualified name |
1. Creating a Method That Receives a Single Parameter | Begin by defining the same elements as you do for methods that do not accept parameters - optional access specifiers, return type for the method, and the method name, In addition, you must include the following items within method declaration parentheses: |
2. Creating a Method That Receives a Single Parameter | 1. The parameter type.. 2. A local name for the parameter... Ex: public static void predictRaise(double salary) |
Local Variable | A variable that is known only within the boundaries of the method. The variables and constants declared within a method are all local to the method |
Creating a Method That Requires Multiple Parameters | You can pass multiple arguments to a method by listing the arguments within the call to the method and separating them with commas. Ex: public static void predictRaiseUsingRate(double salary, double rate) |
Method's Signature | The combination of the method name and the number, types, and order of arguments. A method call must match the called method's signature |
Actual Parameters | The arguments in a method |
Formal Parameters | The variables in the method declaration that accept the values from the actual parameters |
Method Overloading | The ability to execute different method implementations by altering the argument used with the method name |
1. When a Method Ends | A method ends when any of the following events takes place... 1. The method completes all of its statements.. 2. The method throws an exeception(error).. 3. The method reaches a return statement. |
2. When a Method Ends | A return statement causes a method to end and the program's logic to return to the calling method. Also, a return statement frequently sends a value back to the calling method |
Method's Type | The return type for a method. Ex: public static void nameAndAddress()...This method returns no value, so it is type void. Ex: public static int getAge()...The method returns an int, so it is type int. |
is-a Relationship | A relationship in which the object "is a" concrete example of the class |
Instantiation of a Class | An object is an instantiation of a class, or one tangible example of a class |
Creating a Class | you create a class header with three parts... 1. An optional access specifier.. 2. The keyword class.. 3. Any legal identifier you choose for the name of your class. A header for a class that represents an employee might be, public class employee |
Data Fields | The data components of a class. Data fields are variables you declare within a class but outside of any method |
1. Static versus Nonstatic | Consider the following code... public class Emplyee { private int empNum; }... The data field empNum is not preceded by keyword static. If keyword static had been inserted, only one empNum value would be shared by all Employee objects instantiated. |
2. Static versus Nonstatic | Because the empNum field is not preceded by static, when you eventually create, or instantiate, objects from the class, each Employee object can have its own unique empNum. Each object gets its own copy of each nonstatic data field. |
Instance Variable | A nonstatic field is an instance variable for a class |
Private Access | Assigning private access to a field that no other classes can access the field's values, and only method of the same class are allowed to set, get, or otherwise use private variables. Most fields, like empNum in the Employee class are private |
Information Hiding | The principle used in creating private access. Is an important component of object-oriented programming |
1. A Classe's Fields and Method's Access | A class's private data can be changed or manipulated only by a class's own methods and not by methods that belong to other classes. In contrast to fields, most class methods are public, not private. |
2. A Classe's Fields and Method's Access | The resulting private data/public method arrangement provides a means for you to control outside access to your data - only a class's nonprivate methods can be used to access a class's private data |
Mutator Methods | Methods that set or change field values. These methods conventionally start with the prefix set. Ex: setEmpNum(int emp) |
Accessor Methods | Methods that retrieve values. These methods conventionally start with the prefix get. Ex: getEmpNum() |
Instance Methods | Nonstatic methods(methods used with object instantiations) in a class. You can use either static or nonstatic methods with an object, but only nonstatic methods behave uniquely for each object. Cannot use nonstatic methods without object |
Allocating Memory for an Object | To allocate the needed memory for an object, you must use the new operator. Two statements that actually set aside enough memory to hold an Employee as follows... Employee someEmployee; //Next line someEmployee = new Employee(); |
1. Allocating Memory for an Object with Combined Statement | Instead of using two statements, you can declare and reserve memory for someEmployee in one statement, as in the following... Employee someEmployee = new Employee()... |
2. Allocating Memory for an Object with Combined Statement | In this statement, Employee is object's type(as well as class), and someEmployee is name of object. In this statement, someEmployee becomes reference to the object-the name for a memory address where object is held. The Employee() method is a contructor |
Constructor | A special type of method that creates and initializes objects. The name of the constructor is always the same as the name of the class whose objects it constructs. |
Default Constructor | A constructor that requires no arguments. Is created automatically by the Java compiler for any class you create whenever you do not write your own computer. A constructor establishes an object |
Abstract Data Types(ADTs) | A type whose implementation is hidden and accessed through its public methods, Programmers sometimes refer to classes as abstract data types(ADTs) |
Programmer Defined Data Type | It is a type that is not built into the language. A class can be called a programmer defined data type. |
A Client Method | A method that calls another method |
Unreachable Statements | Stat that cannot be executed because the logical path can never encounter them; an unreachable statement causes a compiler error |
Dead Code | A set of statements that are logically unreachable |