Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

Overloading to provide different different behaviour to the same object

Author: Info Campus
by Info Campus
Posted: Apr 06, 2017

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

  1. Method Overloading
  2. 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

{

  1. public static void main(String[] args)

  2. {

  3. System.out.println("main with String[]");

  4. }

  5. public static void main(String args)

  6. {

  7. System.out.println("main with String");

  8. }

  9. public static void main()

  10. {

  11. System.out.println("main without args");

  12. }

  13. }

Output;

main with String[]

main with String

main without args

Advance java training in bangalore

Features in Overloading:

  1. We can overload the static members.
  2. We can overload the non static members.
  3. We can overload the constructor
  4. We can overload the private static and non static members but we have to create object in the same class.

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 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

About the Author

A Real Time Software Training Institute 100% Guaranteed JOB Support Get Real Time Training from Industry Experts Book Free Demo Class

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Info Campus

Info Campus

Member since: Feb 08, 2017
Published articles: 45

Related Articles