Methods: Communicating with Objects

We communicate with objects using methods. Methods are executable code within each object, for which an interface has been established. Sometimes the interface is only for the object itself. Other times it is an interface accessible by other objects. This chapter discusses that topic in detail.

Chapter Summary

  • accessor method 
  • class scope 
  • formal parameter 
  • if statement 
  • if/else statement 
  • inherit 
  • local scope 
  • loop structure 
  • method overloading 
  • method signature 
  • mutator method 
  • multiway selection 
  • override 
  • polymorphism 
  • repetition structure 
  • scope 
  • selection 
  • side effect 
  • while statement 
  • while structure 

Summary of Important Points 

  • A formal parameter is a variable in a method declaration. It always consists of a type followed by a variable identifier. An argument is a value that is passed to a method via a formal parameter when the method is invoked. A method’s parameters constrain the type of information that can be passed to a method. 
  • When an argument of primitive type is passed to a method, it cannot be modified within the method. When an argument of reference type is passed to a method, the object it refers to can be modified within the method. 
  • Except for void methods, a method invocation or method call is an expression which has a value of a certain type. For example, nim.getSticks() returns a int value. 
  • The signature of a method consists of its name, and the number, types, and order of its formal parameters. A class may not contain more than one method with the same signature. 
  • A constructor is a method that is invoked when an object is created. If a class does not contain a constructor method, the Java compiler supplies a default constructor. 
  • Restricting access to certain portions of a class is a form of information hiding. Generally, instance variables are hidden by declaring them private. The class’s public methods make up its interface. 
  • The if statement executes a statement only if its boolean condition is true. The if-else statement executes one or the other of its statements depending on the value of its boolean condition. Multiway selection allows one and only one of several choices to be selected depending on the value of its boolean condition. 
  • The while statement is used for coding loop structures that repeatedly execute a block of code while a boolean condition is satisfied.


SOLUTION 3.1 A method declaration defines the method by specifying its name, qualifiers, return type, formal parameters, and its algorithm, thereby associating a name with a segment of executable code. A method invocation calls or uses a defined method. 

SOLUTION 3.2 A formal parameter is a variable in the method declaration, whose purpose is to store a value while the method is running. An argument is a value that is passed to a method in place of a formal parameter.

Annotation 2020-03-24 154043

Annotation 2020-03-24 153944

Annotation 2020-03-24 154139

Annotation 2020-03-24 154308

Annotation 2020-03-24 154436