Polymorphism

Polymorphism


Polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages according to dictionary meaning. This principle can also be applied to object-oriented programming and languages like the Java language.

Remember any java object that can pass more than one IS-A test can be considered polymorphic.

Other than object of type object, all java objects are polymorphic in that they pass the IS-A test for their own type and for class object.

Polymorphism is an Oops concept which advice use of common interface instead of concrete implementation while writing code

When we program for interface our code is capable of handling any new requirement or enhancement arise in near future due to new implementation of our common interface. If we don't use common interface and rely on concrete implementation, we always need to change and duplicate most of our code to support new implementation

How Polymorphism support in Java
Java has excellent support of polymorphism in terms of method overloading (compile time polymorphism) and method overriding (runtime polymorphism).

Example

public class A{
public String getMessage(){
return "calling A";
}
}
public class B extends A{
public String getMessage(){
return "calling B";
}
}

public class C extends A{
public String getMessage(){
return "calling C";
}
}





No comments:

Post a Comment