Polymorphism in Java

 Polymorphism in Java:-

  • The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. 

  • Polymorphism is considered as one of the important features of Object Oriented Programming. 

  • Polymorphism allows us to perform a single action in different ways. 

  • In other words, polymorphism allows you to define one interface and have multiple implementations. The word “poly” means many and “morphs” means forms, So it means many forms.

Two type of  polymorphism in java:-

1.Compile time Polymorphism
2. Polymorphism

 Compile time polymorphism:-
  • It is also known as static polymorphism.

  •  This type of polymorphism is achieved by function overloading or operator overloading.

Overloading and overriding:-




Method Overloading:-

  • When there are multiple functions with same name but different parameters then these functions are said to be overloaded

  • Functions can be overloaded by change in number of arguments or/and change in type of arguments.

Example 2 :-

By using different types of arguments 

class MultiplyFun

{

  

       static int Multiply(int a, int b)

    {

        return a * b;

    }

  

        static double Multiply(double a, double b)

    {

        return a * b;

    }

}

  

class Main {

    public static void main(String[] args)

    {

  

        System.out.println(MultiplyFun.Multiply(2, 4));

  

        System.out.println(MultiplyFun.Multiply(5.5, 6.3));

    }

}


Example 2:-

By using different numbers of arguments 

class MultiplyFun {

  

       static int Multiply(int a, int b)

    {

        return a * b;

    }

  

        static int Multiply(int a, int b, int c)

    {

        return a * b * c;

    }

}

  

class Main {

    public static void main(String[] args)

    {

        System.out.println(MultiplyFun.Multiply(2, 4));

  

        System.out.println(MultiplyFun.Multiply(2, 7, 3));

    }

}



Operator Overloading:-

Java also provide option to overload operators. 

For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single operator ‘+’ when placed between integer operands, adds them and when placed between string operands, concatenates them.

In java, Only “+” operator can be overloaded:

  • To add integers

  • To concatenate strings

Example:-

class Operator1 {

  

    void operator(String str1, String str2)

    {

        String s = str1 + str2;

        System.out.println("Concatinated String - " + s);

    }

  

    void operator(int a, int b)

    {

        int c = a + b;

        System.out.println("Sum = " + c);

    }

}

  

class Main {

    public static void main(String[] args)

    {

        OperatorOVERDDN obj = new OperatorOVERDDN();

        obj.operator(2, 3);

        obj.operator("joe", "now");

    }

}


Runtime Polymorphism :-

  • It is also known as Dynamic Method Dispatch. 

  • It is a process in which a function call to the overridden method is resolved at Runtime. 

  • This type of polymorphism is achieved by Method Overriding. 

Method overriding:-

  • on the other hand, occurs when a derived class has a definition for one of the member functions of the base class.

  • That base function is said to be overridden.


Example:-

class Parent {

  

    void Print()

    {

        System.out.println("parent class");

    }

}

  

class subclass1 extends Parent {

  

    void Print()

    {

        System.out.println("subclass1");

    }

}

  

class subclass2 extends Parent {

  

    void Print()

    {

        System.out.println("subclass2");

    }

}

  

class TestPolymorphism3 {

    public static void main(String[] args)

    {

  

        Parent a;

  

        a = new subclass1();

        a.Print();

  

        a = new subclass2();

        a.Print();

    }

}







Comments

Popular posts from this blog

Java Package

Life cycle of a Thread and creating thread class

Inheritance in Java