Overloading to provide different different behaviour to the same object
Overloading means same type but differs in number of parameters, type of
parameters or position of parameters.
Parameter : Parameters is nothing but argument which are passed while
defining and calling of methods and constructors.
Method : Method means set of codes which are referring by name, and we
call the method simply by utilising the method name. When we call the method
same line of code executed that is define inside the method.
Constructor : Constructor is also a special type of method in java which
always hold the class name.
It is mainly used to define the object. Once we create an object, then constructor
calls. There is no explicitly need to call the constructor.
Java training center bangalore
Types of overloading: Overloading is of two types
- Method Overloading
- Constructor Overloading
Method Overloading : Method Overloading means same method name
but differs in number of parameters, type of parameters or position of
parameters.
Example
class Run
{
public static void demo(int a)
{
System.out.println("parameter is :"+a);
}
public static void demo(int a,int b)
{
System.out.println("parameter is :"+a,b);
}
public static void demo(double a)
{
System.out.println("parameter is :"+a);
}
public static void demo(int a,double b)
{
System.out.println("parameter is :"+a,b);
}
public static void demo(double a,int b)
{
System.out.println("parameter is :"+a,b);
}
public static void main(String[]args)
{
demo(90);
demo(23,67);
demo(78.9);
demo(45,89.9);
demo(34.8,78);
}
}
Output:
parameter is :90
parameter is :23,67
parameter is :78.9
parameter is :45,89.9
parameter is:34.8,78
java course in bangalore
Benefit of Method Overloading : To provide the different -different behaviour to the same object we go with the method overloading.
Constructor Overloading: Constructor overloading means in the single
class multiple constructor exist with the same name but differs in number o
f parameters, type of parameters or position of parameters.
Class Run
{
Run(int a)
{
System.out.println("parameter is :"+a);
}
Run(int a,int b)
{
System.out.println("parameter is :"+a,b);
}
Run(double a)
{
System.out.println("parameter is :"+a);
}
Run(int a,double b)
{
System.out.println("parameter is :"+a,b);
}
Run(double a,int b)
{
System.out.println("parameter is :"+a,b);
}
Public static void main(String[]args)
{
Run obj1 = new Run(20);
Run obj2 = new Run(34,89);
Run obj3=new Run(67.9);
Run obj4=new Run(56,87.9);
Run obj5=new Run(43.9,23);
}
}
best core training in bangalore
parameter is :20
parameter is :34,89
parameter is : 67.9
parameter is :56,87.9
parameter is :43.9,23
Benefit of Constructor Overloading :
-Constructor overloading is mainly used to provide the flexibility to the objects. That mean we can create the objects in different-different way.
Note: We can overload the main method also.
Example-
class Test
{
-
public static void main(String[] args)
-
{
-
System.out.println("main with String[]");
-
}
-
public static void main(String args)
-
{
-
System.out.println("main with String");
-
}
-
public static void main()
-
{
-
System.out.println("main without args");
-
}
-
}
Output;
main with String[]
main with String
main without args
Advance java training in bangalore
Features in Overloading:
- We can overload the static members.
- We can overload the non static members.
- We can overload the constructor
- We can overload the private static and non static members but we have to create object in the same class.
Rules for overloading
- Overloading can appear in the same class or a subclass.
- 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 we 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.
Java /j2ee classes bangalore
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.
Use of Overloading :
-To save the memory space.
-To increase the consistency and readability of the program.
-We can create more than one function with the same name.
-Method Overloading is mainly used to achieve the compile time polymorphism/static binding/early binding.
This is short detail on the Overloading.To get more knowledge on Java join Infocampus Software Training institute.
Author :Infocampus is a java training center bangalore. It provides java courses on Core and Advance with the live project. Fully practical classes are available for java/j2ee classes.Pay fees in installaments.
Training is given by the experienced trainers on Advance java training. Mock Test and mock interview are conducted every weekend. Every weekend free demo classes are available.
For the free demo call at 9738001024 or for more details on best core java training visit at : http://www.infocampus.co.in/java-training-bangalore.html