Method Overloading
You are allowed to have more than one method with the same name. as long as the methods with the same name take different numbers and/or types of parameters.
All methods with the same function may have the same name. This reduces confusion.
e.g. a method that squares a number, without overloading:
public static int SquareInt(int x)
{ return x * x;}
public static double SquareDouble(double x)
{return x * x;}
With overloading:
public static int square(int x)
{return x * x;}
public static double square(double x)
{return x * x;}
For Java to use one method over another is called "overriding."
Overloaded methods provide for default parameters, and should call each other.
Interfaces as Parameters
Interface: a specification for a class containing method declarations.
A class that is declared to implement the interface must implement those methods exactly as specified.
e.g. Comparable
int compareTo(Object o)
Object a implements Comparable, as well as object b.
int x = a.compareTo(b);
If a > b, x > 0
If a < b, x < 0
If x == b, x == 0
Method "printCompare" takes two arguments and prints to the screen whether one argument is greater than the other. This is typically an easier method to implement.
Welcome
Welcome to my blog.
I will be updating this blog with lecture notes and miscellaneous information for:
Prof. A. Eckford, Section-Z: MWF at 10:30 in R S137
Feel free to leave me a message or send me an e-mail at producap@yorku.ca.
Here is a link for MyMail, Hotmail, G-mail, and PRISM login.
On that note, you must have a PRISM account in order to write the lab tests.
If you have not created a PRISM account yet (e.g., if you received transfer credit for 1020), see the lab monitor in CSEB 1006 as soon as possible.
I will also leave the blog open to comments, etc. much like the CSE forums.
Thanks for visiting.
Phil.
Thursday, January 17, 2008
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment