Method/Function Overloading in Java

In Java, function overloading is called Method Overloading.

Definition: In java method overloading means creating more than one method with same name but with different signature.
i.e., If name of the method remains common but the number and type of parameters are different, then it is called method overloading in Java.

Overloaded Methods:
Here, these methods have same return type and name, but the signature(i.e., the parameter it takes are different).

void sum(int a, int b)
{
System.out.println("Int: Sum of "+a+" and "+b+" is "+(a+b));
}

void sum(float a, float b)
{
System.out.println("Float: Sum of "+a+" and "+b+" is "+(a+b));
}

void sum(String a, String b)
{
System.out.println(a+" "+b);
}

One takes int value, other takes float value and the 3rd takes two string values as parameters.

These parameter type decides which method to call.

Video Tutorial for the working of Method/Funtion overloading in Java


[youtube https://www.youtube.com/watch?v=x7WMSdgMq60]

YouTube Link: https://www.youtube.com/watch?v=x7WMSdgMq60 [Watch the Video In Full Screen.]



Full Source Code:(overLoad.java)

import java.lang.*;

class work
{
void sum(int a, int b)
{
System.out.println("Int: Sum of "+a+" and "+b+" is "+(a+b));
}

void sum(float a, float b)
{
System.out.println("Float: Sum of "+a+" and "+b+" is "+(a+b));
}

void sum(String a, String b)
{
System.out.println(a+" "+b);
}

}

class overLoad
{
public static void main(String args[])
{
work w1 = new work();

w1.sum(10,20);
w1.sum(10.5F,1.5F);
w1.sum("I Love","You!");
}
}

output:
Int: Sum of 10 and 20 is 30
Float: Sum of 10.5 and 1.5 is 12.0
I Love You!

Using Constructors & Member Function to add two numbers: Java

In this Java video tutorial we have covered both ways of adding numbers: using constructors as well as using methods.

Constructors:

Constructor name and the class name must be same.
No return type.
There are 3 types of constructors:
1. Default constructors.
2. Parameterized constructors.
3. Copy Constructors.

In this video tutorials we have shown the use of Parameterized constructors.

calc(int a, int b)
{
no1 = a;  
no2 = b;  
}

Adding and displaying the result:

public void sum()
{
System.out.println("Sum of "+no1+" and "+no2+" is "+(no1+no2));
}

Strings are written inside double quotes and the variables written outside the double quotes will display the value stored in them.
+ (plus) is concatenation operation.
(no1+no2) adds the value present in no1 and no2 and displays the result of addition.

Video Tutorial


[youtube https://www.youtube.com/watch?v=gUXq1psurdk]

YouTube Link: https://www.youtube.com/watch?v=gUXq1psurdk [Watch the Video In Full Screen.]



Object Creation:

/* calc c; // object declaration. It will have null in it.
   c = new calc(20,10); // Construction call
*/ 
   calc c = new calc(30, 10);

Full Java code: Using Constructors & Member Function to add two numbers(add.java)

import java.lang.*;
 
class calc
{
int no1, no2;
 
calc(int a, int b)
{
no1 = a;  // 30
no2 = b;  // 10
}
 
/*
public void assign(int a, int b)
{
no1 = a;  // 30
                no2 = b;  // 10
}
*/ 
public void sum()
{
System.out.println("Sum of "+no1+" and "+no2+" is "+(no1+no2));
}
}
 
class add
{
public static void main(String args[])
{
/* calc c; // object declaration. It will have null in it.
c = new calc(20,10); // Construction call
*/ 
calc c = new calc(30, 10);
//c.assign(30,10);
c.sum();
}
}

Member functions are called with the help of object.
Syntax:
object.memberfuntion();

First Java Program: Basic

This Java tutorial will illustrate the basics of all java programs.

class Welcome

Class is a keyword. Welcome is an identifier or name of the class.
Class Welcome is declaration of class.

class Welcome
{

}

Every class definition in Java begins with an opening brace and ends with a matching closing brace. Note that there is no ending semicolon in Java unlike in C++.

public static void main(String args[])
{
System.out.println("Welcome To Java!");
}

main() is the starting point for the interpreter to begin execution of the program. A java application can have any number of classes but only one of them must include a main method.
void: since main does not return any value;
static: Java is a pure object oriented programming language and hence even main is written inside of a class. main() belongs to the entire class and not a part of any objects of the class, so main is declared as static.
public: methods with public access specification can be accessed even outside the class.

String: is a built-in class, present inside java.lang package.
args[] : is a array variable, used to get the arguments from the user.

System.out.print() or System.out.println() is similar to printf() in C or cout>> in C++.
print() or println() method is a member of the out object, which is a static data member of System class.

Full Java code to print “Welcome to Java!” (Welcome.java)

class Welcome
{
public static void main(String args[])
{
System.out.println("Welcome To Java!");
}
}

Video Tutorial


[youtube https://www.youtube.com/watch?v=lXaFnboq3sk]

YouTube Link: https://www.youtube.com/watch?v=lXaFnboq3sk [Watch the Video In Full Screen.]


[youtube https://www.youtube.com/watch?v=ePG4GE3hmbY]

YouTube Link: https://www.youtube.com/watch?v=ePG4GE3hmbY [Watch the Video In Full Screen.]



Please watch the above video tutorial in full screen.