What is process of defining two or more methods within same class or in subclass that have same name but different parameters declaration?

Although a method name can be any legal identifier, code conventions restrict method names. In general, method names should be verbs and should be in mixed case, with the first letter in lowercase and the first letter of each internal word in uppercase. Here are some examples:
toString compareTo isDefined setX getX
A method name should not be the same as the class name, because constructors are named for the class. The JavaBeans architecture naming conventions further describe how to name methods for setting and getting properties.
Note:  You should refer to Sun Microsystems' code conventions for the Java programming language
What is process of defining two or more methods within same class or in subclass that have same name but different parameters declaration?
and the JavaBeans architecture naming conventions outlined in the JavaBeans specification
What is process of defining two or more methods within same class or in subclass that have same name but different parameters declaration?
.
Typically, a method has a unique name within its class. However, three situations might cause a method to have the same name as other methods in the class or in a superclass: overriding methods, hiding methods, and name overloading.

A method with the same signature and return type as a method in a superclass overrides or hides the superclass method. The section Overriding and Hiding Methods

What is process of defining two or more methods within same class or in subclass that have same name but different parameters declaration?
describes what each means, shows you how to override and to hide methods, and discusses related issues.

The Java programming language supports name overloading for methods, which means that multiple methods in the same class can share the same name if they have different parameter lists. Suppose that you have a class that can draw various types of data (strings, integers, and so on) and that contains a method for drawing each data type. In other languages, you have to think of a new name for each method, for example, drawString, drawInteger, drawFloat, and so on. In the Java programming language, you can use the same name for all the drawing methods but pass a different type of argument to each method. Thus, the data drawing class might declare three methods named draw, each of which takes a different type of argument.

public class DataArtist { ... public void draw(String s) { ... } public void draw(int i) { ... } public void draw(float f) { ... } }
Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample, draw(String s) and draw(int i) are distinct and unique methods because they require different argument types. You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart. The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.


Page 2

The declaration for a method or a constructor declares the number and the type of the arguments for that method or constructor. For example, the following is a method that computes the monthly payments for a home loan, based on the amount of the loan, the interest rate, the length of the loan (the number of periods), and the future value of the loan:
public double computePayment(double loanAmt, double rate, double futureValue, int numPeriods) { double I, partial1, denominator, answer; I = rate / 100.0; partial1 = Math.pow((1 + I), (0.0 - numPeriods)); denominator = (1 - partial1) / I; answer = ((-1 * loanAmt) / denominator) - ((futureValue * partial1) / denominator); return answer; }
This method takes four arguments: the loan amount, the interest rate, the future value and the number of periods. The first three are double-precision floating point numbers, and the fourth is an integer.

As with this method, the set of arguments to any method or constructor is a comma-separated list of variable declarations, where each variable declaration is a type/name pair. As you can see from the body of the computePayment method, you simply use the argument name to refer to the argument's value.

You can pass an argument of any data type into a method or a constructor. This includes primitive data types, such as doubles, floats, and integers, as you saw in the computePayment method, and reference data types, such as classes and arrays. Here's an example of a factory method that accepts an array as an argument. In this example, the method creates a new Polygon object and initializes it from a list of Points (assume that Point is a class that represents an x, y coordinate):
public static Polygon polygonFrom(Point[] listOfPoints) { ... }
The Java programming language doesn't let you pass methods into methods. But you can pass an object into a method and then invoke the object's methods.
When you declare an argument to a method or a constructor, you provide a name for that argument. This name is used within the method body to refer to the data.

The name of an argument must be unique in its scope. It cannot be the same as the name of another argument for the same method or constructor, the name of a local variable within the method or constructor, or the name of any parameter to a catch clause within the same method or constructor.

An argument can have the same name as one of the class's member variables. If this is the case, the argument is said to hide the member variable. Hiding member variables can make your code difficult to read and is conventionally used only within constructors and methods that set a particular member variable. For example, consider the following Circle class and its setOrigin method:

public class Circle { private int x, y, radius; public void setOrigin(int x, int y) { ... } }
The Circle class has three member variables: x, y, and radius. The setOrigin method accepts two arguments, each of which has the same name as one of the member variables. Each method argument hides the member variable that shares its name. So using the simple names x or y within the body of the method refers to the argument, not to the member variable. To access the member variable, you must use a qualified name. See the section Using the this Keyword
What is process of defining two or more methods within same class or in subclass that have same name but different parameters declaration?
for details.

Arguments are passed by value. When invoked, a method or a constructor receives the value of the variable passed in. When the argument is of primitive type, "pass by value" means that the method cannot change its value. When the argument is of reference type, "pass by value" means that the method cannot change the object reference but can invoke the object's methods and modify the accessible variables within the object.

To get a better idea of what this means, let's look at a method called getRGBColor within a class called Pen. This method is attempting to return three values by setting the values of its arguments:

public class Pen { private int redValue, greenValue, blueValue; ... //This method does not work as intended. public void getRGBColor(int red, int green, int blue) { red = redValue; green = greenValue; blue = blueValue; } }
This simply does not work. The red, green, and blue variables exist only within the scope of the getRGBColor method. When that method returns, those variables are gone and any changes to them lost.

Let's rewrite the getRGBColor method so that it does what was intended. First, we need a new type of object, RGBColor, that can hold the red, green, and blue values of a color in RGB space:

public class RGBColor { public int red, green, blue; }
Now we can rewrite getRGBColor so that it accepts an RGBColor object as an argument. The getRGBColor method returns the current color of the pen by setting the red, green, and blue member variables of its RGBColor argument:
public class Pen { private int redValue, greenValue, blueValue; ... public void getRGBColor(RGBColor aColor) { aColor.red = redValue; aColor.green = greenValue; aColor.blue = blueValue; } }
The changes made to the RGBColor object within the getRGBColor method persist after the method returns, because aColor is a reference to an object that exists outside the scope of the method.


Page 3

The JavaTM Tutorial
What is process of defining two or more methods within same class or in subclass that have same name but different parameters declaration?
What is process of defining two or more methods within same class or in subclass that have same name but different parameters declaration?
What is process of defining two or more methods within same class or in subclass that have same name but different parameters declaration?
Start of Tutorial > Start of Trail Search
Feedback Form

Trail: Learning the Java Language
This chapter has been updated to reflect features and conventions of the latest release, JDK 5.0, but it is not yet final. We've published this preliminary version so you can get the most current information now, and so you can tell us (please!) about errors, omissions, or improvements we can make to this tutorial.
With the knowledge you now have of the basics of the Java programming language and creating and using objects, you can learn to write your own classes. In this chapter, you will find information about defining your own classes, including declaring member variables, writing methods, inheriting variables and methods from superclasses, nesting classes within other classes, and so on.

What is process of defining two or more methods within same class or in subclass that have same name but different parameters declaration?
What is process of defining two or more methods within same class or in subclass that have same name but different parameters declaration?
What is process of defining two or more methods within same class or in subclass that have same name but different parameters declaration?
Start of Tutorial > Start of Trail Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.