Polymorphism come from the two Greek words ‘poly’ meaning many and ‘morphs’ meaning forms. The ability to exist in different form is called polymorphism. It is the ability of an object to take on many forms. It gives us the ultimate flexibility in extensibility. The ability to define more than one function with the same name is called polymorphism.
Polymorphism can be achieved in java in two ways. They are
1.Method Overloading Polymorphism or Compile Time Polymorphism or static Polymorphism or
Early binding polymorphism
2. Method Overriding Polymorphism or Run Time Polymorphism or Dynamic Polymorphism or Late binding polymorphism
1.Method Overloading Polymorphism or Compile Time Polymorphism or static Polymorphism or
Early binding polymorphism :
Methods with the same name but different arguments or signatures – return type & no of arguments used in method are defined within a class.Ex: System.out.println() is overloaded in PrintStream class.
Ex:
void func() { ... }
void func(int a) { ... }
float func(double a) { ... }
float func(int a, float b) { ... }
This is known as method overloading in Java.
Examples on Method Overloading
- Same method name
- Same class name
- Different arguments or signature
- Number of argument
- Sequence of argument
- Type of argument
Example 1:
class MoverloadingTest
{
void show()
{
System.out.println("without arguments -1");
}
void show(int a)
{
System.out.println("with arguments -2");
}
public static void main(String[] args)
{
MoverloadingTest t = new MoverloadingTest();
t.show(10);
t.show();
}
}
Output:
ubuntu@ubuntu:~/Desktop/rkjava$ javac MoverloadingTest.java
ubuntu@ubuntu:~/Desktop/rkjava$ java MoverloadingTest
with arguments -2
with out arguments -1
Method Overloading(Static Polymorphism / Static Binding / Early Binding) in Java
Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. It is similar to constructor overloading in Java, that allows a class to have more than one constructor having different argument lists.
Three ways to overload a method
In order to overload a method, the argument lists of the methods must differ in either of these:
1.Number of parameters.
Ex:
This is a valid case of overloading
add(int, int)
add(int, int, int)
Ex:Different Number of parameters in argument list
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
Output:
a
a 10
2.Data type of parameters.
Ex:
add(int, int)
add(int, float)
Ex: Difference in data type of parameters
class DisplayOverloading2
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(int c)
{
System.out.println(c );
}
}
class Sample2
{
public static void main(String args[])
{
DisplayOverloading2 obj = new DisplayOverloading2();
obj.disp('a');
obj.disp(5);
}
}
Output:
a
5
3.Sequence of Data type of parameters.
Ex:
add(int, float)
add(float, int)
Ex: Sequence of data type of arguments
class DisplayOverloading3
{
public void disp(char c, int num)
{
System.out.println("It is char and int type disp method");
}
public void disp(int num, char c)
{
System.out.println("It is int and char type disp method " );
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading3 obj = new DisplayOverloading3();
obj.disp('x', 51 );
obj.disp(52, 'y');
}
}
Output:
It is char and int type disp method
It is int and char type disp method
Example 2:
class Demo
{
public void displayPattern()
{
for(int i = 0; i < 10; i++)
{
System.out.printf("\t %d",i);
}
}
public void displayPattern(char symbol)
{
for(int i = 0; i < 10; i++)
{
System.out.printf("\t %c",symbol);
}
}
}
class Moverloading
{
public static void main(String[] args)
{
Demo d1 = new Demo();
d1.displayPattern();
System.out.println("\n");
d1.displayPattern('#');
}
}
Output:
C:\Users\Student\Desktop\java prg>javac Moverloading.java
C:\Users\Student\Desktop\java prg>java Moverloading
0 1 2 3 4 5 6 7 8 9
# # # # # # # # # #
Example 3:
class Moverloading11
{
public int sum(int num1,int num2)
{
return num1+num2;
}
public int sum(int num1,int num2, int num3)
{
return num1+num2+num3;
}
public static void main(String args[])
{
Moverloading11 obj=new Moverloading11();
System.out.println(obj.sum(20, 15));
System.out.println(obj.sum(81, 100, 10));
}
}
Output:
35
191
2. Method Overriding Polymorphism or Run Time Polymorphism or Dynamic Polymorphism or Late binding polymorphism :
Example on Method Overriding
In Java, run-time polymorphism can be achieved through method overriding. Suppose the same method is created in the super class and its sub-classes. In this case, the method that will be called depends upon the object used to call the method.
- Same method name
- With in Different class
- Same arguments or signature
- Number of argument
- Sequence of argument
- Type of argument
- Inheritance IS-A Relationship
class Xyz
{
void show(String a,int b)
{
System.out.println("with arguments -1");
System.out.println(a + " " + b);
}
}
class MoverridingTest extends Xyz
{
void show(String a, int b)
{
System.out.println("with arguments -2");
System.out.println(a + " " + b);
}
public static void main(String[] args)
{
Xyz x = new Xyz();
x.show("ram" , 111 );
MoverridingTest t = new MoverridingTest();
t.show("sita" , 222 );
}
}
Output:
ubuntu@ubuntu:~/Desktop/rkjava$ javac MoverridingTest.java
ubuntu@ubuntu:~/Desktop/rkjava$ java MoverridingTest
with arguments -1
ram 111
with arguments -2
sita 222
Example:
class Parentclass
{
public void property()
{
System.out.println("Gold| Land| Cash");
}
public void marry()
{
System.out.println("Parent Method : appalamma | subbalaxmi");
}
}
class Childclass extends Parentclass
{
public void marry()
{
System.out.println("Child Method : katrina");
}
}
public class Moverriding
{
public static void main(String[] args)
{
Parentclass p = new Parentclass();
p.marry();
Childclass c = new Childclass();
c.marry();
Parentclass rk = new Childclass();
rk.marry();
}
}
Output:
C:\Users\KANAKA RAJU\Desktop\java 9-5-22>javac Moverriding.java
C:\Users\KANAKA RAJU\Desktop\java 9-5-22>java Moverriding
Parent Method : appalamma | subbalaxmi
Child Method : katrina
Child Method : katrina
Example 2:
abstract class Animal
{
public abstract void makeSound();
}
{
public abstract void makeSound();
}
class Dog extends Animal
{
public void makeSound()
{
System.out.println("bark bark..");
}
}
{
public void makeSound()
{
System.out.println("bark bark..");
}
}
class Cat extends Animal
{
public void makeSound()
{
System.out.println("meow meow..");
}
}
{
public void makeSound()
{
System.out.println("meow meow..");
}
}
class Moverriding
{
public static void main(String[] args)
{
Dog d1 = new Dog();
d1.makeSound();
Cat c1 = new Cat();
c1.makeSound();
}
}
{
public static void main(String[] args)
{
Dog d1 = new Dog();
d1.makeSound();
Cat c1 = new Cat();
c1.makeSound();
}
}
Output:
bark bark..
meow-meow..
bark bark..
meow-meow..
Example 3:
class Bike
{
void run()
{
System.out.println("run");
}
}
class Splendor extends Bike
{
void run()
{
System.out.println("running safely with 60km");
}
public static void main(String args[])
{
Bike b = new Bike();//upcasting
b.run();
}
}
Output:
run
Example 4:
class Bike
{
void run()
{
System.out.println("run");
}
}
class Splendor extends Bike
{
void run()
{
System.out.println("running safely with 60km");
}
public static void main(String args[])
{
Bike b = new Splendor();//upcasting
b.run();
}
}
Output:
running safely with 60km
Example 5:
Java Runtime Polymorphism Example on Shape
class Shape
{
void draw()
{
System.out.println("drawing...");
}
}
class Rectangle extends Shape
{
void draw()
{
System.out.println("drawing rectangle...");
}
}
class Circle extends Shape
{
void draw()
{
System.out.println("drawing circle...");
}
}
class Triangle extends Shape
{
void draw()
{
System.out.println("drawing triangle...");
}
}
class TestPolymorphism2
{
public static void main(String args[])
{
Shape s;
s=new Rectangle(); s.draw();
s=new Circle(); s.draw();
s=new Triangle(); s.draw();
}
}
output:
ubuntu@ubuntu:~/Desktop/rkjava$ javac TestPolymorphism2.javaubuntu@ubuntu:~/Desktop/rkjava$ java TestPolymorphism2
drawing rectangle...
drawing circle...
drawing triangle...
No comments:
Post a Comment