Polymorphism in Java

  • By
  • July 8, 2022
  • JAVA
Polymorphism in Java -

Polymorphism in Java –

As we know java is an “Object-oriented programming language” and also we have some concepts called the Oops concept and this polymorphism is one of the oops concepts.

Let’s now get into the polymorphism concept deeply.

Polymorphism means “Many Forms” –

Many forms mean using the same method in many ways, how can we achieve this polymorphism so we have “Method overloading” .

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

What is Method overloading?

Method overloading is using the same method with different parameters and a different number of parameters.

Ex: add();

      add(int a);

      add(int a,int b);

      add(float a,float b);

      add(double a, double b);

      add(char a, char b);

      add(string name, string last_name );

In the above example, you can see that I have used a method called add(), I have used the same method add so many times but it differs in parameter type and the number of parameters. How this is going to work let’s see that 

Whatever the methods I have used in the class if I want to execute those methods then I need to call them in the main function how the execution is going to happen the execution of the methods depends on the method which we are calling it depends on type and number of parameters like.

Ex: if I call add(5,6);//add(int a, int b) method will be executed.

If I call add(‘A’,’B’);//add(char a,char b) method will be executed.

So else see one program implementation on method overloading.

package JavaPrograms;

 

class Mo

{

public void print(int a)

{

System.out.println(“int=”+a);

}

public void print(int a,int b)

{

System.out.println(“int sum=”+a+b);

}

public void print(float a)

{

System.out.println(“float=”+a);

}

public void print(double a)

{

System.out.println(“double=”+a);

}

public void print(String a)

{

System.out.println(“string=”+a);

}

public void print(char a)

{

System.out.println(“char=”+a);

}

}

public class MethodOverloading {

 

public static void main(String[] args) {

Mo m= new Mo();

m.print(“Method overloadng”);

m.print(5,4);

m.print(3);

m.print(3.0);

m.print(3000.000);

m.print(‘M’);

m.print(5);

}

}

 

OUTPUT:

string=Method overloading 

Int sum= 9

Int=3

double=3.0

double=3000.0

char= M

int 5

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

I have a class called Mo (method overloading) in the above program. Which contains the method called print() which has been used many times but with different parameters. The execution of the methods depends on the call of the method with the arguments. If I call print() with the integer argument the int print() will be executed, Same other method execution depends on the arguments.

 

Another way to achieve polymorphism is “Method overriding”.

What is Method Overriding?

Method overriding is about using the same method in two different classes but with different method definitions how do both classes use the same method because of inheritance (refer to my previous blog for inheritance). 

Consider there is a class called super which consists of a method called print() and there is one more class called base which is inheriting the class super which means base can access the members of the super class so class base accesses the print() and the class is overwriting the print() definition. Now when invoking the print() through the base class object that time the base class print method is going to execute. Why base class print() is getting executed why not super class print(). It’s because when you create a base class object and call the print() method then the JVM will search for the print() method in the object class if it doesn’t get print() in the child class then it will goto parent which is inherited by the child class and execute but in this scenario, the print method is also present in the base class so the preference is given to the child class(base) which has a print method and that will be executed. Let’s implement the one program on this method overriding 

 

class Super

{

void print()

{

System.out.println(“This is from  class Super”);

}

}

class Base extends Super

{

void print()

{

System.out.println(“This is from class Base”);

}

}

public class MethodOverriding {

 

public static void main(String[] args) {

 

        Base base = new Base();

        base.print();

}

}

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

Output:

This is from class Base –

 

In the above program, you can observe that there is print() in both classes, and in the main class I am calling print() by base class object “base. print()” so based on the call of the method the output we are getting is “This is from a class base” because the preference is given to the class child(Base). If you want to call the parent(Super) class print() method then you need to create the object for the parent class then super class print() is going to execute. So this was polymorphism. 

 

Author:

Chetna Malagi

Call the Trainer and Book your free demo Class  Call now!!!

| SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

Submit Comment

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

*
*