Reputation: 93
I am just wondering what the difference would be in creating a method with no parameter vs one with a String parameter. To my understanding, the method with no defined parameter can be called anywhere in the program where as the one with the String parameter only works for the String (unsure)? For example
public static void exampleOne(){
}
vs
public static void exampleOne(String x){
}
Upvotes: 0
Views: 393
Reputation: 2051
It's based on what requirement you have. Like what are the different types of variables that you would like to have single definitions! For example, for a single method, you may have different types of business logic based on argument type or a number of arguments to do. This concept is called Method overloading.
The best example I found is inbuild Java class.
System.out.println(int x);
System.out.println(boolean x);
System.out.println(String x);
You can see the inbuild implementation of these methods.
Upvotes: 1
Reputation: 3454
if you add some code to these methods, the terminologie will pop up clearly =)
public static void exampleOne(){
System.out.println("hello World");
}
and
public static void exampleTwo(String x){
System.out.println("hello World, "+x);
}
then you can parametrize the outcome from exampleTwo
by providing the String x.
exampleOne(); //will print Hello World
exampleTwo("BipoN"); //will print Hello World BipoN
Upvotes: 1
Reputation: 2648
Given example is overloaded function falls under OOPS concept.
public class Booking{
public void flightTicket(){
return "ticket";
}
public void flightTicket(String meal){
return "ticket with "+meal;
}
}
Above example describe for booking flight ticket. If you want to just book the flight ticket first function will be called while with meal another will.
How to call those functions
Booking booking = new Booking();
booking.flightTicket(); //will print ticket
booking.flightTicket("Veg"); // will print ticket with Veg
Upvotes: 1