Ticker

6/recent/ticker-posts

Polymorphism & its types

 


Polymorphism: The term polymorphism comes from two Greek words: poly, meaning many, and morphs, meaning forms. In OOP, polymorphism allows you to perform various operations by using methods with the same names. In Java, this is possible by changing the implementation of the method. Polymorphism can be either static or dynamic, depending on how the method is invoked in a class. In dynamic polymorphism, the behavior of a method is decided during runtime, whereas in static polymorphism, it is decided during compilation. Static polymorphism is also known as early binding because binding is performed during compilation. Dynamic polymorphism is also known as late binding, because here binding occurs during runtime, depending on the type of object. Let’s look at the two types of polymorphism in detail.

Static Polymorphism:  that is exhibited during compilation is called static polymorphism. In static polymorphism, the Java compiler binds the method calls with method code at compile-time. This type of polymorphism is therefore also known as compile-time polymorphism. Method overloading is a mechanism in which we can define various methods with the same name. However, there is one condition: these methods should have different signatures. Method signatures represent the method name, the number of parameters the method has and the type of method parameters. The Java compiler differentiates the various methods with the same names by their method signatures.

Implementing Static Polymorphism

class staticPoly

 {

void product(int x, int Y)

 {

System.out.println("Product of two numbers:"+(x*y));

}

void product(int x, int y, int z)

 {

System.out.println("Product of three numbers:"4.(x*y*z); }

public static void main(string args[])

} }

StaticPoly obj=new StaticPoly();

 obj.product(20,10);

obj.product(5,6.7);

}

}


 For more understanding you can also read below mentioned topic from online:

polymorphism java interview questions


Post a Comment

0 Comments