October 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Object Signatures

  • October 6, 2005
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

This series, The Object-Oriented Thought Process, is intended for someone just learning an object-oriented language and wants to understand the basic concepts before jumping into the code or someone who wants to understand the infrastructure behind an OOP language they are already using. Click here to start at the beginning of the series.

In keeping with the code examples used in the previous articles, Java will be the language used to implement the concepts in code. One of the reasons that I like to use Java is because you can download the Java compiler for personal use at the Sun Microsystems Web site http://java.sun.com/. You can download the J2SE 1.4.2 SDK (software development kit) to compile and execute these applications and I will provide the code listings for all examples in this article. I have the SDK 1.4.0 loaded on my machine. I will also provide figures and the output (when appropriate) for these examples. See the previous articles in this series for detailed descriptions for compiling and running all the code examples.

In the previous article in this series, you investigated a potential conflict between two of the most fundamental object-oriented concepts—inheritance and encapsulation. You mitigated this conflict by packaging your classes. By organizing your classes in packages, you can create much more secure designs for your object models and thus your applications.

By creating these packages, you provide the users with very specific guidelines so that they can utilize your programming interfaces. In fact, unless you are very clear as to what these interfaces are, there will be no way for anyone to effectively employ the classes that you meticulously create. Thus, it is vitally important to put a significant amount of thought into how to design these interfaces—which are defined by what are called object signatures.

What's in a Signature?

Although many people might not even be able to define the term, object signatures may well be one of the most important object-oriented concepts that you will learn. Whenever you design, implement, use, test, and so forth, you must pay attention to the signatures of the object involved. Actually, when you speak of object signatures, you are really talking about the signatures of the object's methods that are required to use the object.

It is helpful to think of the signatures of an object as the interfaces to that object. This means that you would use these signatures to invoke object services. Signatures are often known by other names. That is because signatures are nothing more than how you call (invoke) a method. In a structured environment, the signature can be thought of in a similar manner—how you invoke a procedure, routine, or function. Look at an example familiar to most structured programmers—using math functions.

In a structured environment, the math functions are most likely kept in a corresponding math library. In an object-oriented environment, the concept is similar. Specifically, Java uses what are called packages. Thus, the math functionality is provided by methods in the java.lang.Math package. The methods correspond to the services provided by the language via its objects. Now, focus on one of the methods in the java.lang.Math package, the sqrt() method. More to the point; first consider how a method to calculate a square root is used and how it is implemented.

Interface/Implementation

Several times in previous articles in this series, you touched upon the issue of interfaces and implementations. In this section, you see how these concepts are put into practice.

Consider the following problem:

You want to provide a programming interface that, when invoked, will return the square root of a number.

Most people, when asked, are not able to provide the formula for calculating a square root. Although they may understand what a square root is, they may not have the foggiest idea of how it is derived. Fortunately for you, due to the forethought of the Java compiler and language designers, developers do not have to understand these details; they only need to know how to use the service provided by the square root method provided by the language.

For example, as I indicated earlier, in the java.lang.Math package there is a method called sqrt(). This method provides the service of calculating the square root of a number sent via the parameter line and returning the result. The question is how to take advantage of this service so that you can use it appropriately. The answer to this question is via the signature of the sqrt() method. This is why signatures are so important; they provide the mechanism to utilize an object service.

How do you find the proper service? All services are found in the Application Programmers Interface of the language (the API specifications). To find the Java API specifications, in this case for v1.4.2, you can visit the site: http://java.sun.com/j2se/1.4.2/docs/api/index.html. Figure 1 shows what this page looks like.

Figure 1

This Web page contains all of the APIs available when developing a Java 2, standard edition application. In short, this Web page contains all of the method signatures (services) available when developing a Java 2, standard edition application. How do you find the one that you are interested in—a service that provides the result for a square root calculation? Click on the Index link at the top of the page—see Figure 2.

Figure 2

Then, click on the S in the alphabet provided (because it is likely that a method to calculate the square root will start with sq. When you scroll through these methods, you come across one that is indeed called sqrt()—see Figure 3.

Figure 3

Here we have found the signature of the method that we are interested in:

sqrt(double) - Static method in class java.lang.Math
        Returns the correctly rounded positive square root of a double value.

To utilize the sqrt() service in an application, you need focus only on the signature. As an exercise, define a class called Calculate that will perform various mathematical operations. One of these operations is to calculate a square root. Listing 1 shows what the design of this class might look like.

Listing 1: The Calculate Class

// Class Calculate

public class Calculate { private double result; public double squareRoot (double value){ return result; } }

In this class, you have provided the method signature for your squareRoot() service.

public double squareRoot (double value)

This method's name is squareRoot(); a single double parameter is passed, and it returns a double value.

Recall from earlier columns in this series that a signature is basically three things:

  • Method Name
  • Parameter List
  • Return Value

There is a point of contention that should be mentioned at this point. Although you must carefully consider the return type in method signatures when designing, the Java compiler does not check the return type as part of the overload process (some languages do include the return type). This may seem somewhat confusing; look at a simple example using your squareRoot() method.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel