Polymorphism
Poly means 'many' morphism means 'form' polymorphism means one thing many form.
Compile time polymorphism also known as function overloading
Run time polymorphism also known as function overriding
Function overloading: Function overloading is a compile time polymorphism in which
- The name of the function is same
- Type of parameter or number of parameter must be different
class addittion
{
int c;
float c1;
void add(int a, int b)
{
c=a+b;
System.out.println(c);
}
void add(int a, int b, int d)
{
c=a+b+d;
}
void add(float a, float b)
{
c1 = a+b;
System.out.println(c1);
}
public static void main(String k[])
{
addition ad = new addition();
ad.add(5,6,7);
ad.add(5,6);
ad,.add(5.6f,6.6f);
}
}
Function overriding: Function overriding is a run time polymorphism and it is possible only through the inheritance.
{
void show()
{
System.out.println("car class");
}
}
class bmw extends car
{
void show()
{
System.out.println("BMW");
}
public static void main(String k[])
{
bmw bm = new bmw();
bm.show();
car c = new car();
c.show();
}
}
Difference between function overloading and function overriding.
Overloading
Function overriding: Function overriding is a run time polymorphism and it is possible only through the inheritance.
- Must be inheritance (IS-A relation)
- Name of the functions are same.
- The type of parameter and number of parameter are same.
{
void show()
{
System.out.println("car class");
}
}
class bmw extends car
{
void show()
{
System.out.println("BMW");
}
public static void main(String k[])
{
bmw bm = new bmw();
bm.show();
car c = new car();
c.show();
}
}
Difference between function overloading and function overriding.
Overloading
- In overloading function name are same but parameter type and parameter number must be different.
- It can perform in same and another class.
- It is a compile time polymorphism.
- static method can be overloaded. example: main method of java can be overloaded
- The name of the function and type of parameter and number of parameter all are same.
- It can only be perform through the inheritance.
- it is a run time polymorphism.
- static method cannot be override. example: main method of java cannot be override.

No comments:
Post a Comment