Search Java Tutorials

Custom Search

What You Will Want to Complete This Tutorial?

You must have at least 1st and 2nd pre-requirements to start the tute. Other requirements will require for last chapters only. These all are free to use and download!
  1. Java Development Kit Standard Edition (JDK / J2SE) Any Version(www.java.com)
  2. Any Text Editor (Note Pad quite enough or get Notepad++ from (http://notepad-plus.sourceforge.net)
  3. Java Run Time Environment (JRE) Any Version(www.java.com)
  4. Java Enterprise Edition (J2EE) for advanced developing (For last few chapters)(www.java.com)
  5. Java Wireless Toolkit for Mobile Application Development (For Mobile Application Development Chapters) (www.java.com)
  6. Tomcat Web Server to run Servlets. (www.tomcat.apache.org/)
Your operating system may be any version of Windows.
What will you learn (As a summary)?
  1. How to build console and windowed applications using Java
  2. How to build web page elements with Java Applets
  3. How to build Database Programs with Java
  4. How to build enterprise applications with Beans
  5. How to add Email functions to Java programs
  6. How to program smart devices (Phone, PDA etc…) with Java

How to Overloading Methods in Java?

Overloading involves writing methods with the same name, but different argument lists.

Ex: If you have enough information. You can do the same task “telling time” in following three different ways.

Public static void tellTime(hour, min); // Only hour and minutes
Public static void tellTime(hour, min, sec); // Only hour, minutes and also seconds
Public static void tellTime(date, hour, min, sec); // Date of the month, and all.

Java can do the same thing. If method receives enough information it can do the same task in different ways. This is what you called as overloading and it provides “Polymorphism” features in java.

Sample Program:
Case: We will define three variables and three methods with same name. It’s your computers work to catch the right method according to arguments it receives.

public class MethodOverloading
{
public static void main(String[] args)
{
String aText1 = "Text 1";
String aText2 = "Text 2";
String aText3 = "Text 3";

aMethod(aText1);
aMethod(aText1,aText2);
aMethod(aText1,aText2,aText3);
}

public static void aMethod(String text)
{
System.out.println("I received one text only: " + text);
}

public static void aMethod(String text, String text2)
{
System.out.println("I received two text only: " + text + " and " + text2);
}

public static void aMethod(String text, String text2, String text3)
{
System.out.println("I received all texts: " + text + ", " + text2 + " and " + text3);
}
}


The Output is:



Tip: Polymorphism helps you write systems that are flexible andmodification resistant. It allows a method to take on "many forms" which in turn makes it re-usable and therefore more flexible.

Tip: Be careful, If you have a mixed collection arguments of integer, long like data types, there may be unexpected errors. To avoid such errors, when assigning values use letter ‘L’ for ‘long’ values, and ‘f’ for float values.

Ex: long aLong = 11338833L;
Ex: float aFloat = 34.7f;

How to Overload Constructors?

Constructor methods can receive arguments and be overloaded. If you explicitly create a constructor for a class, the automatically created constructor does not exist.

No comments: