Polymorphism Part-1 (Method Overloading)


Polymorphism

"Poly" means "many" and "morph" means "form"

One task can be performed in different ways. For example: Creating something in different ways like in Automobile industry companies make different models of cars they can use this feature of polymorphism to achieve this functionality.

In java, we use method overloading (Compile time, Early binding, Static binding) and method overriding (Run time, Late binding, Dynamic binding).

In java, we use method overloading and method overriding to achieve polymorphism.
Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.

Method Overloading:

It is a feature of creating two or more methods with having same name but different arguments for example if you need to do addition but number of arguments may vary then you should use method overloading in which you will create multiple methods with same method name but different argument types + different data types (if required). Method overloading increases the code readability.

Different ways to overload the method:
  1. By changing number of arguments
  2. By changing the data type
  3. Sequence of Data type of arguments
1.) Method Overloading : By changing number of arguments

class PrintOverloading1
{
    public void show(char c)
    {
         System.out.println(c);
    }
    public void show(char c, int number) 
    {
         System.out.println(c + " "+number);
    }
}
class demo1
{
   public static void main(String args[])
   {
       PrintOverloading1 obj = new PrintOverloading1();
       obj.show('j');
       obj.show('j',10);
   }
}
Output:

j
j 10

In the above example, method named show is overloaded based on number of arguments. Method have same name but different arguments/parameters

2.)
Method Overloading : By changing the data type


class PrintOverloading2
{
    public void show(char c)
    {
        System.out.println(c);
    }
    public void show(int c)
    {
       System.out.println(c );
    }
}

class demo2
{
    public static void main(String args[])
    {
        PrintOverloading2 obj = new PrintOverloading2();
        obj.show('j');
        obj.show(5);
    }
}
Output:

j
5

In the above example, show method is overloaded on the basis of different data type being used like first show method is having Char type where as second method have int type.

3.) Method Overloading : Sequence of data type of Arguments

class PrintOverloading3
{
    public void show(char c, int number)
    {
        System.out.println(c,number);
    }
    public void show(int number,char c)
    {
       System.out.println(number,c);
    }
}

class demo2
{
    public static void main(String args[])
    {
        PrintOverloading3 obj = new PrintOverloading3();
        obj.show('j',4);
        obj.show(5,'j');
    }
}
Output:

j4
5j

In the above example method with same name is created but having different sequence of data types like first show method having char and int type where as second method having int and char type.

If you still have any question you can reach me any time through email (jigyasu2010@hotmail.com)

Comments

Popular posts from this blog

How Method Overloading Helping in Java - Part 9

Important Java Interview Questions - Part 7

Access Modifiers in java