DEV Community

loading...

JAVA: OOP (Polymorphism)

supriya2371997 profile image Supriya Kolhe Updated on ・4 min read

Q1: OOPS: Polymorphism?
Q2: Compile-time polymorphism?
Q3: Run-time polymorphism?
Q4: Facts about Polymorphism?
Q5: Advantages?
Q6: Disadvantages?
Q7: Method Overloading?
Q8: Method Overloading?

1. Polymorphism:-

1.1 having the ability for a message or data to be processed in more than one form
1.2 It is derived from two Greek words, that is, poly and morphs. “poly” means many and “morphs” means forms. Henceforth, polymorphism implies many forms.
1.3 for e.g. a function to read secured classes - class_category() can take up different types of arguments like - class_category(FirstClass), class_category(SecondClass), class_category(Distinction), etc.

Alt Text

2. Compile-time polymorphism:-

2.1 performed by method overloading
2.2 also called as Static binding/Early binding

3. Run-time polymorphism:-

3.1 performed by method overriding
3.2 also called Dynamic binding/Dynamic Method Dispatch/Late binding, where

a call to an overridden method is resolved at runtime rather than compile-time OR
implemented dynamically when a program is being executed is called run-time polymorphism.

3.3 Here the overridden method is called through a reference variable of a parent class
3.4 Runtime polymorphism can't be achieved by data members

4. Facts about Polymorphism:-

4.1 all the working code in various classes does not require to know about the class being used by it, since their way of usage is the same.
4.2 For example, suppose there is a button and we know that pressing it will do something, but unaware of the output or the reference of its use. So the conclusion is, either way, it will not affect the way it is used.

5. Advantages:-

5.1 helps programmer to reuse the code and also the classes that once written to be tested and implemented
5.2 one variable name can contain values of multiple data types such as int, float etc.
5.3 improved readability of the program

6. Disadvantages:-

6.1 developers may find it difficult to implement polymorphism in codes
6.2 may affect performance

if the machine needs to decide which method or variable to invoke. and as these decisions are taken at run time.

6.3 reduces the readability of the program.

7. Method overloading:-

7.1 same method name & different parameters
7.2 it is performed within class
7.3 the main advantage of it is increased readability

when we must perform only one operation, having the same name of the methods.

ways to overload the method:
option 1: changing the number of arguments
option 2: changing the data type
option 3: changing both the number of arguments and data type

option 1: changing the number of arguments:-
-suppose you have to perform a multiplication operation of the given number but there can be n number of arguments, then if you create a method such as mult1(int, int) for two parameters and mult2(int, int, int) for three parameters then it may be difficult for the programmer to understand the behaviour of the block i.e. method because of the different name. In such situations, option 1 can be used.
-Github Link to code

class MethodOverloading
{
    static int mult (int a,int b)
    {
        return a*b;
    }
    static int mult(int a,int b,int c)
    {
        return a*b*c;
    }
}
class MethodOverloadingDifferentNumberOfArguments
{
    public static void main(String[] args)
    {
        System.out.println(MethodOverloading.mult(13,13));
        System.out.println(MethodOverloading.mult(9,9,9));
    }
}
Enter fullscreen mode Exit fullscreen mode

Here we are creating static methods so that there is no need to create an instance for calling methods.

option 2: changing the data type:-
-suppose you have to perform a multiplication operation of the given number where given numbers can be of any data types. Then we can create methods such as mult(int, int), mult(float, float) and so on.
-Github Link to code

class MethodOverloading
{
    static int mult (int a,int b)
    {
        return a*b;
    }
    static double mult(double a,double b,double c)
    {
        return a*b*c;
    }
}
class MethodOverloadingChangingDataType
{
    public static void main(String[] args)
    {
        System.out.println(MethodOverloading);
        System.out.println(MethodOverloading);
    }
}
/*15
35.721*/
Enter fullscreen mode Exit fullscreen mode

option3: changing both the number of arguments and data type:-
-sometimes we may need to use the method which is having both different numbers of arguments and data types. These methods are called or invoked with the value of the same data type and number of parameters used.
-Github Link to code

class MethodOverloading
{
    static int one (int a)
    {
        return a*a;
    }//return square
    static int two(int a,int b)
    {
        return a*b;
    }//return multiplication
    static int three(int a,int b,int c)
    {
        return a+b+c;
    }//return addition
}
class MethodOverloadingBoth
{
    public static void main(String[] args)
    {
        System.out.println(MethodOverloading.one(5));
        System.out.println(MethodOverloading.two(2,3));
        System.out.println(MethodOverloading.three(2,3,4));
    }
}
/*25
6
9*/
Enter fullscreen mode Exit fullscreen mode

8. Method overriding:-

-used to achieve the runtime polymorphism or dynamic binding.
how to achieve method overriding:

a. the method name must be same as in the parent class
b. the parameters must be same as in the parent class
c. there must be an inheritance i.e. IS-A relationship

-Github Link to code

class MethodOverridingParent
{
    int a=10;
    public void run()
    {
        System.out.println("MethodOverridingParent class's method");
    }
}
class MethodOverridingChild1 extends MethodOverridingParent
{
    int a=9;
    public void run()
    {
        System.out.println("MethodOverridingChild1 class's method "+a);
    }
}
class MethodOverridingChild2 extends MethodOverridingParent
{
    public void run()
    {
        System.out.println("MethodOverridingChild2 class's method "+a );
    }
}
class MethodOverriding
{
    public static void main(String[] args)
    {
        MethodOverridingParent m1=new MethodOverridingChild1();
        m1.run();
        MethodOverridingParent m2=new MethodOverridingChild2();
        m2.run();
    }
}
/*MethodOverridingChild1 class's method 9
MethodOverridingChild2 class's method 10*/
Enter fullscreen mode Exit fullscreen mode

Here, child classes will override the default behaviour provided by superclass and some of its own specific behaviour. As you can see MethodOverridingChild1 is giving the priority to it's own variable a first in order to perform the operation. and since MethodOverridingChild2 does not have any of it's own variable a, it is accessing the values of it's parent class.

Please comment if you have any feedback or suggestions

Discussion (1)

Some comments have been hidden by the post's author - find out more

Forem Open with the Forem app