Polymorphism in java:-
Polymorphis is feture of the java un wich the poly me ans many and merf means forms.In the polymorphism using one methd we can do various types of opeerations in different enviroments.
Types of Polymorphism:
There are two types of polymerphism in the java,
1.static polymephism
2.Dynamic polymerphism
Static polymorphism:-
In the static polymophism the method cans be overloaded that means we can use the same method with the different parameters and different return type.It is also know as the early binding.
Exapmle of methiod overloading:-
In the Methidd overloading gthe metiod argume ts should be different from each other
like this:-
public void add(int a)
public int sum(int a,int b)
programs:-
class Methodoverloading
{
public void sertData(char a)
{
system.out.pierntln("c");
}
public int setData(char a,int num)
{
System.out.println("+c+"+num);
}
public sratic void main(STring args[])
{
Methodoverloading md=new Methodoverloading();
md.setData(a);
md.setData(a,4);
}
}
Dynamic Polymorphism:-
In the Dynamic polymerphism the methods can be overrided thats means the method should have the same name and same metod return type and the same arguments.The Method ovveride is possible only in case of the inhertance.It is also known as the late binding.
Exapmle of method Overriding:
Exapmle 2:-
class A
{
int data;
A()
{
increment();
}
public void increment()
{
data++;
}
}
class B extends A
{
B()
{
increment();
}
public void increment()
{
data+=10;
}
}
class Demo1
{
public static void main(String args[])
{
B bb=new B();
bb.increment();
System.out.println("data value is"+bb.data);
}
}
Next-Dynamic Method dispatch in java(click here)
Exapmle of methiod overloading:-
In the Methidd overloading gthe metiod argume ts should be different from each other
like this:-
public void add(int a)
public int sum(int a,int b)
programs:-
class Methodoverloading
{
public void sertData(char a)
{
system.out.pierntln("c");
}
public int setData(char a,int num)
{
System.out.println("+c+"+num);
}
public sratic void main(STring args[])
{
Methodoverloading md=new Methodoverloading();
md.setData(a);
md.setData(a,4);
}
}
Dynamic Polymorphism:-
In the Dynamic polymerphism the methods can be overrided thats means the method should have the same name and same metod return type and the same arguments.The Method ovveride is possible only in case of the inhertance.It is also known as the late binding.
Exapmle of method Overriding:
| class X { public void showMessage(String msg) { System.out.println("base calss Massage is:"+msg); } } class Y extends X { @Override public void showMessage(String s) { System.out.println("child class Message is"+s); } } class Demo2 { public static void main(String args[]) { Y aa=new Y(); aa.showMessage("i am in the main"); } } |
Exapmle 2:-
class A
{
int data;
A()
{
increment();
}
public void increment()
{
data++;
}
}
class B extends A
{
B()
{
increment();
}
public void increment()
{
data+=10;
}
}
class Demo1
{
public static void main(String args[])
{
B bb=new B();
bb.increment();
System.out.println("data value is"+bb.data);
}
}
Differenece between the method overloading and method overriding
Method Overloading
|
Method overriding
|
1.This is the part of the polymorphism
2.This is alos known as late binding
3.Methoid name may not be same
4,Independed on the inheritance
5.Arguments of the methods shoud not be same
|
1.This is alos the part of the polymorphism
2.This is alos known as early binding
3.Method name should be same
4.Without inheritance the method overriding is not possible
5.Arguments of the method should be same
|


No comments:
Post a Comment