Java Polymorphism

The process of representing one form in multiple forms is known as Polymorphism.
Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.
Polymorphism is not a programming concept but it is one of the principal of OOPs. For many objects oriented programming language polymorphism principle is common but whose implementations are varying from one objects oriented programming language to another object oriented programming language.

How to achieve Polymorphism in Java ?

In java programming the Polymorphism principal is implemented with method overriding concept of java.
Polymorphism principal is divided into two sub principal they are:
· Static or Compile time polymorphism
· Dynamic or Runtime polymorphism
The process of representing one form in multiple forms is known as Polymorphism.
Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.
Polymorphism is not a programming concept but it is one of the principal of OOPs. For many objects oriented programming language polymorphism principle is common but whose implementations are varying from one objects oriented programming language to another object oriented programming language.

How to achieve Polymorphism in Java ?

In java programming the Polymorphism principal is implemented with method overriding concept of java.
Polymorphism principal is divided into two sub principal they are:
· Static or Compile time polymorphism
· Dynamic or Runtime polymorphism
Static polymorphism in Java is achieved by method overloading and Dynamic polymorphism in Java is achieved by method overriding
Note: Java programming does not support static polymorphism because of its limitations and java always supports dynamic polymorphism.
Example:
public abstract class Human {

    public abstract String getGender();
}
class Male extends Human{
    @Override
    public String getGender() {
        return "male";
    }}
class Female extends Human{
    @Override
    public String getGender() {
        return "female";


    }}
Static polymorphism in Java is achieved by method overloading and Dynamic polymorphism in Java is achieved by method overriding
Note: Java programming does not support static polymorphism because of its limitations and java always supports dynamic polymorphism.
Example:
public abstract class Human {

    public abstract String getGender();
}
class Male extends Human{
    @Override
    public String getGender() {
        return "male";
    }}
class Female extends Human{
    @Override
    public String getGender() {
        return "female";


    }}
أحدث أقدم