What You Will Learn |
---|
|
In Java it is possible to define two or more methods, within the same class, that share the same name, as long as their parameter declarations are different. Notice in the example below that three print methods have been defined.
public void(String name) { System.out.println(name); } public void(String name, String id) { System.out.println(name + " " + id); } public void(String name, String id, String classification) { System.out.println(name + " " + id + " " + classification); }
When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java implements polymorphism. In object-oriented programming, polymorphism (from the Greek meaning "having multiple forms") is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a method to have more than one form. In the example above, from a user's point of view, there is one print method but has three different forms.
Method overloading is one of Java's most useful features. When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call. Thus, overloaded methods must differ in the type and/or number of their parameters. While overloaded methods may have different return types, the return type alone is insufficient to distinguish two versions of a method. When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call. Here is a simple example that illustrates method overloading.
class Shape { // calculates area of rectangle public double(double length, double width) { return length * width; } // calculates area of circle public doublearea(double radius) { return Math.PI * radius * radius; } } public class MyProgram { public static void main(String[] args) { Shape rectangle = new Shape(); System.out.println("Area of Rectangle = " + rectangle.area(10, 20); Shape circle = new Shape(); System.out.println("Area of Circle = " + circle.area(5); } }area
The Shape class defines two methods both with the name area. The first method calculates the area of rectangle and has two parameters representing its length and width.
public double area(double length, double width) { return length * width; }
The second method calculates the area of circle and has one parameter representing its radius.
public double area(double radius) { return Math.PI * radius * radius; }
A common application for method overloading is in the defining of class constructors. Recall that a constructor is a special method for initializing a new instance of a class. The constructor methods for a class have the same name as the class. Look at the following example.
class Dog { // instance variables private String breed; private String name; private int age; public() { breed = ""; name = ""; age = 0; } publicDog(String b) { breed = b; name = ""; age = 0; } publicDog(String b, String n) { breed = b; name = n; age = 0; } publicDog(String b, String n, int a) { breed = b; name = n; age = a; } } public class MyProgram { public static void main(String [] args) { Dog dog1 = new Dog(); Dog dog2 = new Dog("Golden Retriever"); Dog dog3 = new Dog("Collie", "Lassie"); Dog dog4 = new Dog("German Shepherd", "Max", 3); } }Dog
The Dog class defines four constructors. Each constructor contains a different number of parameters. Java determines which constructor to use when instantiating a Dog object by looking at the type and/or number of parameters.
Many classes defined in Java provide more than one constructor. This allows programmers more control over the initial state of their objects when they are instantiated. As an example, the table below lists all of the constructors defined for the JButton class that is part of Java's Swing widget toolkit.
JButton() | Creates a button with no set text or icon. |
JButton(Action a) | Creates a button where properties are taken from the Action supplied. |
JButton(Icon icon) | Creates a button with an icon. |
JButton(String text) | Creates a button with text. |
JButton(String text, Icon icon) | Creates a button with initial text and an icon. |
The JButton class provides five different constructors for creating buttons.