Martrinex Learning Center
HOME CONTENTS
 
This website is underconstruction.
Author: Martin Sykes
Challenge: I have challenged myself to 1 article per day, and framework improvements! Enjoy
Challenge end: unknown

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]
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]
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.

Add Comment

Help the web master by commenting on the tutorials, thankyou.
Name:
Email: (this will not be shown)
Comment:
  Send email to admin, don't post.

 

Comments

Nobody has commented on this article.
Be the first, the webmaster needs comments good or bad to improve this site.



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.
Martrinex Learning Center[X]
  Introduction Beginner Intermediate

Important useful well commented source codes
Please read the import tutorial to know how to use them.

Source