Methods Overloading

Methods Overloading

In java it is possible to create methods that have the same name but different parameter lists and different definitions. This is called method overloading. Method overloading is used when objects are required to perform similar tasks but using different input parameters. When we call a method in an object, java matches up the method name first and the number and type of parameters to decide which one of the definitions to execute. This process is known as polymorphism.

 

Program illustrations for methods overloading

Class room

{

Float length;

Float breadth;

Room(float x, float y ) // constructor 1

{

Length = x;

Breadth = y;

}

Room ( float x ) //constructor 2

{

Length = breadth = x ;

}

Intarea()

{

Return(length x breadth);

}

}

 

 

In the above example we are overloading the method room(). An object representing a rectangular room well be created as

Room room1 = new room( 25.0,15.0); // using constructor1

If the room is square , we may create the corresponding object as

Room room2 = new room ( 20.0); // using  constructor2

 

Leave Comment

Important Topics

Title
OOPS
Data Types
JAVA
JVM
Command Line Args
Machine Neutral
Scope of Variables
Operators :
Generic Type Casting
IF Else
Switch Statement
The while statement
The do statement
The for statement
Classes, objects and methods
Constructors
Methods Overloading
Static Members
Nesting of methods
Inheritance : Extending a Class
Overriding methods
Overriding methods
Final variable and methods
Abstract class in Java
Visibility control