Java method overloading tutorial with example


Method Overloading in java

The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures
Overloading is also known as Compile time polymorphism

Example
public class Student {
	
	String name;
	int age;
	String email;
	
	public void setData(String name,int age)
	{
		this.name=name;
		this.age=age;
	}
	
	public void setData(String name,int age, String email)
	{
		this.name=name;
		this.age=age;
		this.email=email;
	}
	
	public void display()
	{
		System.out.println(name);
		System.out.println(age);
		System.out.println(email);
	}
	
	public static void main(String[] args) {
		Student s1=new Student();
		s1.setData("Shanthi", 20);
		Student s2=new Student();
		s1.setData("Veera", 25,"veera@candidjava.com");
		
	}
}


Rules for overloading

1. Overloading can appear in the same class or a subclass

2. Overloaded methods MUST change its number of argument or its type. 
When declaring two or more methods in same name complier differentiate them by its arguments, so you cannot declare two methods with the same signature

3. Overloaded methods CAN have different return type.
The compiler does not consider return type when differentiating methods, so it?s legal to change return type of overloaded method

4. Overloaded methods CAN change the access modifier.
Similarly overloaded method can have different access modifiers also.

5. Overloaded methods CAN declare new or broader checked exceptions.

Legal overloading
Overloaded method should have different argument
public class Student {
	
	public void add(int i, int j)
	{
		
	}
	
	public void add(int i)
	{
		
	}
	
}

It's legal for Overloaded method to change only its type
public class Student {
	
	public void add(int i)
	{
		
	}
	
	public void add(float i)
	{
		
	}	
}

Overloaded method can change its return type
public class Student {
	
	public void add(float i)
	{
		
	}
	
	public int add(long i)
	{
		return 0;
		
	}
}


Overloaded method can have different access modifiers
public class Student {
	
	
	public int add(long i)
	{
		return 0;
		
	}
	
	private void add(char c)
	{
		
	}
}

Overloaded method can declare new or checked exception
public class Student {
	
	public void add(int i)
	{
		
	}

	public void add(short s)throws Exception
	{
		
	}
}



Illegal overloading

You cannot overload a method by changing only its return type
public class Student {
	
	public void add(int i, int j)
	{
		
	}
	
	public int add(int i, int j) // Compilation error - method already defined
	{
		
	}
}



Related Post

Comments

Ava Labarbera
         Hi My name is Sergey and I am the founder of Creative Bear Tech (https://creativebeartech.com), a lead generation and software business founded in The city of london, UK. I have identified your business on Facebook and felt that you could definitely benefit from our services as we deal with extremely similar organisations. We currently have more than 15,000 customers and I am in the process of expanding our offering by opening offices in the U.S.A. as well as the Baltic States. I would really love to see you and candidjava.com become our next customer! Below are a couple of our most popular solutions that you may locate beneficial for your business. 1. Top Quality B2B Databases and Email Marketing Lists for over 7,000 particular niches and mini specific niches (most popular with companies that have a wholesale offering). 2. SEO software. If you are technology savvy, you can easily make use of our Online search engine Scraper and Email Extractor to scrape your very own sales leads for your specific niche. Many clients use it for discovering guest posting prospects for their internet site Search Engine Optimisation (over 2,000 active users). 3. Instagram Management Software for organic Instagram followers, likes and comments. This is the absolute most famous software at the moment and has over 7,000 active users. 4. Search Engine Optimization Services. We also offer Search Engine Optimization services on Sweaty Quid Freelance Marketplace (sweatyquid.com). We mainly provide link building as we have a massive PBN of over 25,000 internet sites. I would like to give you 25% off your next order with us as a way of welcoming you on-board. Please apply voucher code HELLO2020 for your 25% off any purchase. Valid for 7 days only. If you want to speak to me, feel free to contact me via https://creativebeartech.com/content/contact-us. My private e-mail plays up occasionally so contact form enquiry would be most ideal. You can also talk to me on +447463563696 (UK phone, GMT time zone). Kind regards Sergey Greenfields Ceo of Creative Bear Tech Flat 9, 1 Jardine Rd, St Katharine's & Wapping, London E1W 3WD, United Kingdom +447463563696 https://creativebeartech.com

©candidjava.com