|
Technical Java. Applications for Science and Engineering Authors: Palmer G. Published year: 2003 Pages: 105-108/281 |
Abstract MethodsIn Chapter 7, we introduced abstract classes as a way to define the framework for a collection of related classes. Among other things, an abstract class can declare abstract methods. An abstract method is designated by the abstract keyword in the method declaration. Abstract methods can only be defined in abstract classes. An abstract method declaration consists of the method name , return type, parameter list, throws clause (if any), and a semicolon. There are no braces and no method body. Abstract methods are used to impose a certain structure on subclasses of an abstract class. The abstract method provides the basic method syntax to which subclasses must adhere . Nonabstract subclasses must provide an implementation of any abstract methods defined in superclasses. How the methods are implemented is up to the subclass. The only restriction is that the method name, return type, and input parameter list must match the abstract method declaration. Example: Abstract MethodsTo see abstract methods in action, look at the example in the "Abstract Classes" section of Chapter 7 where the Shape class defines two abstract methods intended to compute the area and circumference of a 2-D geometric shape. |
Final MethodsFinal methods, like final classes and variables , cannot be overridden. A final method is designated as such by using the final keyword in the method declaration. Final methods are used to protect certain functionality from being altered by subclasses. An example of a final method is the wait() method from the Thread class. This method notifies a thread in a waiting state that the condition it is waiting for has been met. You don't want people messing around with threading functionality, so this method is declared final . |
The native and synchronized KeywordsMethods can also be declared to be native or synchronized . A native method is one that is written in a programming language other than Java. This book focuses on Java code development, but you may very well have legacy code that you would like to incorporate into your Java programs. For more information on utilizing native code, see Essential JNI: Java Native Interface by Rob Gordon. A synchronized method is one that ensures thread-safe data access. In other words, two threads would not be able to simultaneously change the value of a variable in a synchronized method. Synchronization becomes a concern when developing multithreaded programs. As that is not a focus of this book, we won't spend any significant time on synchronized methods. For an informative chapter on synchronization, consult Java How-to Program by Harvey M. Deitel and Paul J. Deitel. |
Method ChainingAs we discussed earlier in this chapter, an instance method is called on an object. Methods can also have a reference type as their return type. It is possible to use the return value of one method as the reference on which another is called. What's more, this operation can be performed in a single executable statement. This is called method chaining or cascading and is a way to call multiple methods successively. Method chaining is provided as a convenience. While it can make your Java code more compact, it doesn't add any functionality and can make a code listing somewhat more difficult to follow. You don't have to chain methods, but the capability is there for you if you want it. Example: Using Method ChainingOne way to convert a String to a primitive double type is to first convert the String to a Double object and then convert the Double object to a primitive double . The Double class provides the following two methods to achieve this goal ” public static Double valueOf(String str) public double doubleValue() The valueOf() method returns a Double object corresponding to the specified String . The doubleValue() method can then be called on the Double object returning a double value. Because the return value of valueOf() is the calling type of doubleValue() the method calls can be chained.
public class ChainDemo
{
public static void main(String args[]) {
// You can convert the type in two steps
Double d = Double.valueOf("45.3");
double value = d.doubleValue();
value = value*3.0;
System.out.println("value is " + value);
// Or you can chain the two steps together
value = Double.valueOf("45.3").doubleValue();
value = value*3.0;
System.out.println("value is " + value);
}
}
Output ” value is 135.9 value is 135.9 |
|
Technical Java. Applications for Science and Engineering Authors: Palmer G. Published year: 2003 Pages: 105-108/281 |