Recent

4.Polymorphism in Java



Polymorphism In Java
It means one name, many form Or When one work is done in many ways. For example: draw a shape, now shape can be anything circle, rectangle or ellipse.
Polymorphism in OOPS are of two types:
·         Compile time polymorphism: Whenever an object is bound with their functionality at compile time, this is known as compile time polymorphism. 
·         Run time polymorphism: Whenever an object is bound with their functionality at run time, this is known as run time polymorphism.Example : Campus interview- a work/project is assigned to the candidates after the training.
Compile Time Polymorphism

There are two ways to implement compile time polymorphism:
·         via function overloading
·         via operator overloading

>Java does not support operator overloading explicitly
>Java does not support compile time polymorphism. Because each non static function of a class in Java is virtual implicitly and virtual keyword is used in OOP for dynamic binding. So binding always happens at runtime.
>All static functions binds to their functionality at compile time and non static functions at runtime.

Runtime Polymorphism
As operator overloading is not supported by Java explicitly. So, runtime polymorphism is achieved via function overloading.

Achieving polymorphism via function overloading:
We have 5 things to define a function:
1.    Access specifier
2.    Access modifier
3.    Return Type
4.    function name
5.    arguments
In this access specifier and modifier does not play any role in function overloading and we cannot change the function name.
So, we have to change the arguments carefully to achieve function overloading.

NOTE: In Java on calling time only nameand arguments are used.So two functions
with same arguments and different returntype will be treated as similar
functions.So we have to change only arguments to achieve function overloading.

1) Achieving function overloading by changing the number of arguments in each function
For Example:

void show()
void show(int x)
void show(intx, long l)
void show(int x, long l, char ch)

Just like these functions, we can change the number of arguments to achieve function overloading.

Example:

class Temp
{

void show()
{
System.out.println("Default");
}

void show(int x)
{
System.out.println("\n\n" +x);
}

void show(int x, String name)
{
System.out.println("\n\n" +x);
System.out.println(name);
}

public static void main(String[] args)
{
Temp t = new Temp();
t.show();

t.show(101);

t.show(101, "Javacp");
}
}
OUTPUT:
Default
101
101
Javacp

2) Achieving function overloading by keeping the arguments same for each function:
If we want to keep the number of arguments same in each function and still we want to overload them, then change the data type of their arguments.

For Example:

Void show(int x)
Void show(char ch)
Example:


class Temp
{

void show(int x)
{
System.out.println(x);
}

void show(String name)
{
System.out.println("\n\n"+name);
}

public static void main(String[] args)
{
Temp t = new Temp();

t.show(101);

t.show("Javacp");
}
}

OUTPUT:
101
Javacp

RULES:
·         Never use same arguments in two functions with same types, not even in the vice versa manner.
·         Because In Java a smaller value is automatically type promoted in a higher data type variable if its corresponding variable not found.
·         In that case, the compiler will throw an ambiguity error.

For Example:

Void show(int x, long l)
Void show(long l, int x)


Example:
class Temp
{

void show(int x, long l)
{
System.out.println("int: "+x);
System.out.println("long: "+l);
}

void show(long l, int x)
{
System.out.println("\n\n int: "+x);
System.out.println("long: "+l);
}

public static void main(String[] args)
{
Temp t = new Temp();

t.show(10, 10);

t.show(10, 10);
}
}

Output:
Temp.java:20: error: reference to show is ambiguous t.show(10,10);
both method show(int,long) in Temp and method show (long,int) in Temp match
Temp.java:22: error: reference to show is ambiguous t.show(10,10);
both method show(int,long) in Temp and method show (long,int) in Temp match
2 errors

No comments:

Post a Comment

Bug-dbug Designed by Bugdbug - Developed by Belson Raja Copyright © 2016

Theme images by Bim. Powered by Blogger.
Published By Bugdbug India