method-overloading

Method overloading is a feature of java in which one class can have more than one method with the same name but different parameter list(either data type or the number of arguments should be different). * If only return type is different then it can’t be considered as function overloading. It can be called function overloading also. Function overloading is very useful in a class where one operation(task) can be performed with different types of arguments with the same name. Here is an example of function overloading.

Develop a Program that illustrate method overloading concept. Read about the concept here

class Object
{
	public int Add(int a, int b)
	{
		return (a + b);
	}
	public int Add(int a, int b, int c)
	{
		return (a + b + c);
	}
	public int Add(int a, int b, int c, int d)
	{
		return (a + b + c + d);
	}
}

public class Practical15
{
	public static void main(String[] args)
	{
		Object o = new Object();

		System.out.println("Add 1 => " + o.Add(4, 5)); 
		System.out.println("Add 1 => " + o.Add(4, 5, 6));
		System.out.println("Add 1 => " + o.Add(4, 5, 6, 7));
	
	}
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here