5.11. Method Abstraction and Stepwise Refinement

 
[Page 158 ( continued )]

Chapter Summary

  • Making programs modular and reusable is one of the central goals in software engineering. Java provides many powerful constructs that help to achieve this goal. Methods are one such construct.

  • The method header specifies the modifiers , return value type , method name , and parameters of the method. The modifier, which is optional, tells the compiler how to call the method. The static modifier is used for all the methods in this chapter.

  • A method may return a value. The returnValueType is the data type of the value the method returns. If the method does not return a value, the returnValueType is the keyword void .

  • The parameter list refers to the type, order, and number of the parameters of a method. The method name and the parameter list together constitute the method signature . Parameters are optional; that is, a method may contain no parameters.

  • A return statement can also be used in a void method for terminating the method and returning to the method's caller. This is useful occasionally for circumventing the normal flow of control in a method.

  • The arguments that are passed to a method should have the same number, type, and order as the parameters in the method definition.

  • When a program calls a method, program control is transferred to the called method. A called method returns control to the caller when its return statement is executed or when its method-ending closing brace is reached.

  • A method with a nonvoid return value type can also be invoked as a statement in Java. In this case, the caller simply ignores the return value. In the majority of cases, a call to a method with return value is treated as a value. In some cases, however, the caller is not interested in the return value.


    [Page 159]
  • Each time a method is invoked, the system stores parameters, local variables , and system registers in a space known as a stack . When a method calls another method, the caller's stack space is kept intact, and new space is created to handle the new method call. When a method finishes its work and returns to its caller, its associated space is released.

  • A method can be overloaded. This means that two methods can have the same name as long as their method parameter lists differ .

  • The scope of a local variable is limited locally to a method. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used, and it must be initialized before it is referenced.

  • Method abstractio n is achieved by separating the use of a method from its implementation. The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method. This is known as information hiding or encapsulation.

  • Method abstraction modularizes programs in a neat, hierarchical manner. Programs written as collections of concise methods are easier to write, debug, maintain, and modify than would otherwise be the case. This writing style also promotes method reusability.

  • When implementing a large program, use the top-down or bottom-up coding approach. Do not write the entire program at once. This approach seems to take more time for coding (because you are repeatedly compiling and running the program), but it actually saves time and makes debugging easier.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net