Download Method Overloading

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts

Go (programming language) wikipedia, lookup

Object-oriented programming wikipedia, lookup

C Sharp (programming language) wikipedia, lookup

Falcon (programming language) wikipedia, lookup

C++ wikipedia, lookup

C Sharp syntax wikipedia, lookup

Java performance wikipedia, lookup

Class (computer programming) wikipedia, lookup

Java (programming language) wikipedia, lookup

Design Patterns wikipedia, lookup

Name mangling wikipedia, lookup

Scala (programming language) wikipedia, lookup

String (computer science) wikipedia, lookup

Object lifetime wikipedia, lookup

Java syntax wikipedia, lookup

String literal wikipedia, lookup

Covariance and contravariance (computer science) wikipedia, lookup

Smalltalk wikipedia, lookup

Transcript
Computer Programming I
COP 2210
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.
Material originally created by Greg Shaw, modified by Caryl Rahn.
I.e., there are 7 different ways to create a Rectangle object)
Material originally created by Greg Shaw, modified by Caryl Rahn.