* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Method Overloading
Falcon (programming language) wikipedia , lookup
Go (programming language) wikipedia , lookup
Design Patterns wikipedia , lookup
String literal wikipedia , lookup
Java syntax wikipedia , lookup
Scala (programming language) wikipedia , lookup
String (computer science) wikipedia , lookup
Covariance and contravariance (computer science) wikipedia , lookup
Java (programming language) wikipedia , lookup
Class (computer programming) wikipedia , lookup
Name mangling wikipedia , lookup
Java performance wikipedia , lookup
Object lifetime wikipedia , lookup
Object-oriented programming wikipedia , lookup
Computer Programming I
COP 2210
Instructor: Greg Shaw
Method Overloading
Overloaded methods are two or more methods of the same class
with the same name but different signatures. I.e., they must
have different numbers of parameters or different types of
parameters, or both.
Overloaded methods may NOT differ only in the type returned.
When an overloaded method is called, Java determines exactly
which one to call by examining the argument list.
In
object-oriented
languages
such
as
Java,
class
constructors are commonly overloaded to allow objects to be
created with different numbers of initial values provided.
See OverloadedConstructors.java, which features our old friend
the BankAccount class, but with two constructors:
1.
public BankAccount (String acctNum, double initialBalance)
{
accountNumber = acctNum ;
balance = initialBalance ;
}
Above is the familiar two-argument constructor. It requires
us to pass a String and a double, which are used to
initialize the instance variables accountNumber and balance.
2.
public BankAccount(String acctNum)
{
accountNumber = acctNum ;
balance = 0 ;
}
This constructor requires only one String argument. The
BankAccount object created will have the account number we
specify, but will have a “default” initial balance of 0.0.
(If you consult the Java API Documentation for the Rectangle
class, you will see that it has 7 “overloaded” constructors.
I.e., there are 7 different ways to create a Rectangle object)