Polymorphism in Java

Two types of Polymorphism
1. Compile-time Polymorphism : Method Overloading
1. Run-time Polymorphism : Method Overriding

Methods in Java

Java Method is a collection of statements that are grouped together to
perform a specific task.
• A method must be declared within a class.

 

Types of Methods:

In Java, there are two types of methods:
• Standard Library Methods: These are built-in methods in Java that are
available to use. Such as, print(), nextInt(), sqrt() and many more
• User-defined Methods: We can create our own method based on our
requirements.

Syntax of Defining a function
modifier return_type methode_name (parameter list)
{
// body of the method
}

Here,
• Modifier: it defines the access type of the method
• Return_type: specifies the type of data returned by the method
• Methode_name: name of the method
• Parameter_list: list of parameters. It is the type, order and number of
parameters of the method
• Body_of_method: defines what the method does

 

Categories of User-Defined Methods:
For better understanding of arguments and return type in methods,
user-defined methods can be categorized as:
1. Method with no arguments and no return value
2. Method with arguments but no return value
3. Method with no arguments but with return value
4. Method with arguments and return value.

 

Note: *Without using object, If we want to call any method without any object, then we have to declare the method as static.

 

Method Overloading in Java

 

When a class has two or more methods by same name but different parameters, it is known as method overloading.
• Method overloading increases the readability of the program.

 

Different ways to overload the method
• There are two ways to overload the method in java
1. By changing number of arguments
1. By changing the data type

1. By changing number of arguments
public class MethodOverloading {

void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(int a,int b,int c)
{
System.out.println(a+b+c);
}
public static void main(String args[]){
MethodOverloading obj = new MethodOverloading();
obj.sum(20,20);
obj.sum(10,20,30);
}
}

2. By changing data type of argument
public class MethodOverloading2 {
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(double a, double b)
{
System.out.println(a+b);
}
public static void main(String args[]){
MethodOverloading2 obj=new MethodOverloading2();
obj.sum(10.5,10.5);
obj.sum(20,20);
}
}

Why Method Overloading is not possible by changing the
return type of method only?
❑ In java, method overloading is impossible by changing the method’s return type
only because of ambiguity. Let’s see how ambiguity may occur:
class Adder{
static int add(int a,int b){return a+b;}
static double add(int a,int b){return a+b;}
}
class Test{
public static void main(String[] args){
System.out.println(Adder.add(11,11));//ambiguity
}} //compile time error

 

Can we overload java main() method?
❑ Yes, by method overloading. You can have any number of main methods in a
class by method overloading. But JVM calls main() method which receives
string array as arguments only. Let’s see the simple 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”);}
}

 

Important Points to remember
❑ Two or more methods can have the same name inside the same class if they
accept different arguments. This feature is known as method overloading.
❑ Method overloading is achieved by either:
▪ changing the number of arguments.
▪ or changing the data type of arguments.
❑ It is not method overloading if we only change the return type of methods.
There must be differences in the number of parameters.

 

 

 

 

By Md Jakaria Nur

Software Engineer

Leave a Reply

Your email address will not be published. Required fields are marked *