polymorphism :--
--------------------
polymorphism mean exiting objects behaviour in many forms
There are two type of polymorphism
signature 1)No of orgumantes 2)Types of orguments 3)order of arguments
In below program we have three print methods each with different arguments. When you properly overload a method, you can call it providing different argument lists, and the appropriate version of the method executes
1)complile time / static /method overloading:
-----------------------------------------------------
Example
-------------
public class MethodOverload {
public void add(int a, int b){
System.out.println(a+b);
}
public void add(int a, int b, int c){
System.out.println(a+b+c);
}
public void add(double a,double b, double c){
System.out.println(a+b+c);
}
public static void main(String[] args) {
MethodOverload mo=new MethodOverload();
mo.add(10, 20);
mo.add(20, 30, 50);
mo.add(1.245, 4.567, 7.890);
}
}
2)Run time polymorphism:-
---------------------------------
If two are more methods haveing same name and same arguments available in the super class and sub class
Example:
-----------
public class A {
public void addition(int a,int b){
System.out.println(a+b);
}
public static void main(String[]args){
A ja=new A();
ja.addition(20, 50);
}
}
public class B extends A{
public void addition(int a,int b){
System.out.println(a+b);
}
public static void main(String[] args) {
A ja1=new A();
ja1.addition(40, 30);//70
B ja2=new B();
ja1.addition(55, 35);//90
}
}
--------------------
polymorphism mean exiting objects behaviour in many forms
There are two type of polymorphism
signature 1)No of orgumantes 2)Types of orguments 3)order of arguments
In below program we have three print methods each with different arguments. When you properly overload a method, you can call it providing different argument lists, and the appropriate version of the method executes
1)complile time / static /method overloading:
-----------------------------------------------------
Example
-------------
public class MethodOverload {
public void add(int a, int b){
System.out.println(a+b);
}
public void add(int a, int b, int c){
System.out.println(a+b+c);
}
public void add(double a,double b, double c){
System.out.println(a+b+c);
}
public static void main(String[] args) {
MethodOverload mo=new MethodOverload();
mo.add(10, 20);
mo.add(20, 30, 50);
mo.add(1.245, 4.567, 7.890);
}
}
2)Run time polymorphism:-
---------------------------------
If two are more methods haveing same name and same arguments available in the super class and sub class
Example:
-----------
public class A {
public void addition(int a,int b){
System.out.println(a+b);
}
public static void main(String[]args){
A ja=new A();
ja.addition(20, 50);
}
}
public class B extends A{
public void addition(int a,int b){
System.out.println(a+b);
}
public static void main(String[] args) {
A ja1=new A();
ja1.addition(40, 30);//70
B ja2=new B();
ja1.addition(55, 35);//90
}
}
No comments:
Post a Comment