Polymorphism:
Polymorphism is the ability of an object to take many forms. It is an object programming language that allow to use the variable in different type at different time. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
It is important to know that the only possible way to access an object is through a reference
variable. A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed.
The reference variable can be reassigned other objects provided that it is not declared final.
The type of the reference variable would determine the methods that it can invoke on the object.
A reference variable can refer to any object its declared type or any subtype of its declared
type. A reference variable can be declared as a class or interface type.
Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
It is important to know that the only possible way to access an object is through a reference
variable. A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed.
The reference variable can be reassigned other objects provided that it is not declared final.
The type of the reference variable would determine the methods that it can invoke on the object.
A reference variable can refer to any object its declared type or any subtype of its declared
type. A reference variable can be declared as a class or interface type.
Compile time polymorphism:
1. In compile time polymorphism call is resolved by the compiler
2. It is also known as static binding early binding and overloading as well.
3. Method overloading is a compile time polymorphism where more than one method share the same name with differnt parameter or signature and different return type
4. It is Achieved by function overloading and operator overloading
5. It produce fast execution because that method that need to be executed is known early at the compile time
6. Compile time polymorphism is less flexible as all thing executed at compile time
7. Inheritance is not invoked.
2. It is also known as static binding early binding and overloading as well.
3. Method overloading is a compile time polymorphism where more than one method share the same name with differnt parameter or signature and different return type
4. It is Achieved by function overloading and operator overloading
5. It produce fast execution because that method that need to be executed is known early at the compile time
6. Compile time polymorphism is less flexible as all thing executed at compile time
7. Inheritance is not invoked.
Runtime polymorphism:
2. It is also known as Dynamic binding, late binding and overriding as well.
3. Method overloading is the runtime polymorphism having the same method with same parameter or signature but associated with different classes.
4. It is achieved by the virtual function and pointer.
5. It provide slow execution as compared to early binding because the method that need to be executed is known as runtime
6. Run time polymorphism is more flexible as all thing execute at runtime
7. Inheritance is involved
Program (compile time )
class SimpleCalculator
{
int add(int a, int b)
{
return a+b;
}
int add(int a, int b, int c)
{
return a+b+c;
}
public class Demo
{
public static void main(String args[])
{
SimpleCalculator obj = new SimpleCalculator();
System.out.println(obj.add(10, 20));
System.out.printin(obj.add(10, 20, 30)
}
}
Output:
30
60
Runtime polymorphism program:
class ABC
{
public void myMethod0
{
System.out.println("Overridden Method");
}
}
public class XYZ extends ABC
{
public void myMethod
{
System.out.println("Overiding Method")
}
public static void main(String args[])
{
ABC obj = new XYZ();
obj.myMethod();
}
READ MORE PAGES:
Tags
C plus plus