Polymorphism in Java

Polymorphism in Java : 

What is Polymorphism?

Polymorphism is the ability of an object to take more than one forms. It is one of the important concept of object-oriented programming language. JAVA is object-oriented programming language which support the concept of polymorphisms.

Alternatively, it is defined as the ability of a reference variable to change behavior according to what object instance it is holding.

This allows multiple objects of different subclasses to be treated as objects of a single parent class, while automatically selecting the proper methods to apply to an object based on the child class it belongs.

poly11

There are two types of polymorphism in Java:

1) Compile-time polymorphism (static binding)
2) Runtime polymorphism (dynamic binding)

Method overloading is an example of compile time polymorphism, while method overriding is an example of runtime polymorphism.
poly2

1. Compile time Polymorphism:

The type of polymorphism that is implemented when the compiler compiles a program is called compile-time polymorphism. This type of polymorphism is also called as static polymorphism or early binding.

Method Overloading:

If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. Method overloading is performed within class.

If we must perform only one operation, having same name of the methods increases the readability of the program.

There are three ways to overload the method in java
• By changing number of arguments
• By changing the data type
• By changing both number of arguments and data type

a. Method overloading by changing number of argument:

Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as x(int,int) for two parameters, and y(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs.

Polymorphism in Java – Example1:
In the following program, two methods are created, first add1() performs addition of two numbers and second add1() performs addition of three numbers.

In following example, we are creating static methods so that we don’t need to create instance for calling methods.

class Addition
{
static int add1 (int a,int b)
{
return a+b;
}
static int add1(int a,int b,int c)
{
return a+b+c;
}
}
class xyz
{
public static void main(String[] args)
{
System.out.println(Addition.add1(13,13));
System.out.println(Addition.add1(9,9,9));
}
}Output:
26
27

b. Method Overloading by changing the data type:

Sometime there is need to use the same method which is having different data type. In the following program, we have created two methods that differs in data type. The first add1() method receives two integer arguments and second add1() method receives two double arguments.

class Addition
{
static int add1 (int a,int b)
{
return a+b;
}
static double add1(double a,double b)
{
return a+b;
}
}
class xyz1
{
public static void main(String[] args)
{
System.out.println(Addition.add1(13,13));
System.out.println(Addition.add1(9.5,9.5));
}
}Output:
26
19

c. Method Overloading by changing both number of argument and data type:

Sometimes we may need to use the method which is having the different number of argument and different data type. In the following program, both number of argument and data type is to be changed. Here the method do() is overloaded 3 times: first having one int parameter, second one has 2 int parameters and third one is having double arg. The methods are invoked or called with the same type and number of parameters used.

class Overload1
{
void do (int a)
{
System.out.println (“a: ” + a);
}
void do (int a, int b)
{
System.out.println (“a and b: ” + a + “,” + b);
}
double do(double a)
{
System.out.println(“double a: ” + a);
return a*a;
}
}
class Overload2{
public static void main (String args [])
{
Overload1 Obj = new Overload1();
double result;
Obj.do(20);
Obj.do(20, 30);
result = Obj.do(4.5);
System.out.println(“Output is: ” + result);
}
}a: 20
a and b: 20,30
double a: 4.5
Output is: 20.25

2. Run-time Polymorphism :

The type of polymorphism which is implemented dynamically when a program being executed is called as run-time polymorphism. The run-time polymorphism is also called dynamic polymorphism or late binding.

Core Java Training

Method Overriding in Java

Method overriding is used to achieve the runtime polymorphism or dynamic binding. In method overriding the method must have the same name as in the parent class and it must have the same parameter as in the parent class. If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. Method overriding occurs in two classes that have IS-A (inheritance) relationship.

Example 1 of method overriding

In the following program, we have defined the do() method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method are the same, and there is IS-A relationship between the classes, so there is method overriding.

//parent class
class Animal
{
//define method
void do()
{
System.out.println(“Animal is eating food”);
}
}
//Create child class
class dog extends Animal
{
//define the same method as in the parent class
void do()
{
System.out.println(“Dog is eating food “);
}public static void main(String args[])
{
dog obj = new dog();//creating object
obj.do();//calling method
}
}Output:
Dog is eating food

Example 2 of Method Overriding

An example or program given below from real world where Vehicle is parent class. It can have Vehicle class, and its specialized child classes like bike and car. These subclasses will override the default behavior provided by Vehicle class and some of its own specific behavior.

public class Vehicle
{
public void run()
{
System.out.println(“some speed”);
}
}class bike extends Vehicle
{
public void run()
{
System.out.println(“speed is slower than car”);
}
}class car extends Vehicle
{
public void run()
{
System.out.println(“speed is faster than bike”);
}
}// Next, run() method will be called, depends on type of actual instance created on runtimepublic class Demo
{
public static void main(String[] args)
{
Vehicle v1 = new bike();
v1.run();
Vehicle v2 = new car();
v2.run();
}
}Output:
speed is faster than bike
speed is slower than car

Advantage of polymorphism:

• It helps programmers to reuse the code, classes, methods written once, tested and       implemented. They may be reused in many ways.

• The single variable name can be used to store variables of multiple data types such as   Int, Float, double, Long etc).

• Polymorphism helps in reducing the coupling between different functionalities.

• Method overloading can be implemented on constructors allowing different ways to initialize objects of a class. This enables you to define multiple constructors for handling different types of initializations.

• Method overriding works together with inheritance to enable code reuse of existing classes without the need for re-compilation.

Disadvantage of polymorphism:

• One of the main disadvantages of polymorphism is that developers find it difficult to implement polymorphism in codes.

• Run time polymorphism can lead to the performance issue where machine needs to decide which method or variable to invoke so it basically degrades the performances as decisions are taken at run time.

• Polymorphism reduces the readability of the program. One needs to identify the runtime behavior of the program to identify actual execution time.

Leave a reply:

Your email address will not be published.

Site Footer