Construct and Overloading
Construct
Time to cover to small but important elements of Java, first off I neglected to mention construct, this is the initial function called on one of your classes when it is created with 'new', this may be important to set initial values for the class for example. A constructor has the same name as the class and no return type Not even void the reason for this is fairly obvious the return is already the entire class...
Eg: PERSON person = new PERSON();, new PERSON() returns an instance of PERSON it cannot return anything else...
Code:
// usual class declaration
PERSON person = new PERSON("martin","sykes");
class PERSON{
String strForename;
String strSurname;
// the construct
public /* nothing here */ PERSON(String forename, String surname){
// set initial variables
strForename = forename;
strSurname = surname;
// not return
}
}
Overloading
Overloading is another cool feature of Java, Java does not have optional params unlike BASIC it has something much better.
BASIC:
sub PERSON(optional forename as string="", optional surname as string="")
strForename = forename
strSurname = surname
end sub
JAVA:
Code:
public PERSON(){
strForename = "";
strSurname = "";
}
public PERSON(String forename, String surname){
strForename = forename;
strSurname = surname;
}
But I wrote the same name twice, wouldn't I get an "ambigius name" error like BASIC? No so long as the parameters are different, Java knows which one to call, this allows for ALOT more flexibility then BASIC's is it there is it no aprouch.
I showed this example on a constructor, it is where you will use it the most but it works just well with functions.
To make sure
Overloading:
Remember back when we learn't functions? we had Add2 and Add3? well now we know they can have the same name, make a new function named 'add' which can be overloaded to add 2,3,4 or 5 variables!
[click for answer]
Code:
int add(int num1, int num2){
return doadd(num1,num2,0,0,0);
}
int add(int num1, int num2, int num3){
return doadd(num1,num2,num3,0,0);
}
int add(int num1, int num2, int num3, int num4){
return doadd(num1,num2,num3,num4,0);
}
int add(int num1, int num2, int num3, int num4, int num5){
return doadd(num1,num2,num3,num4,num5);
}
int doadd(int num1, int num2, int num3, int num4, int num5){
return num1+num2+num3+num4+num5;
}
Here is where you ask me ok, so how is that better then optional? Not liking to be out done, make a function to add two integers and return a int, and another to add two floats and return a float.
[click for answer]
Code:
int add(int num1, int num2){
return (num1+num2);
}
float add(float num1, float num2){
return num1+num2;
}
Try that in basic, all you can do is two functions or use varient, if you use varient you put yourself at risk of my trying to do: add("a","b");
Constructors
The next tutorial demonstrates the use of constructors better.
Copyright (c) 2008, Martin Sykes.
Learning Center is a branch of Martrinex Systems, Martrinex.net
Do not copy any materials from this site without permission from the auther.
Add Comment
Comments