i came to know that there are 2 types of ploymophism in java language ;

first one the runtime and the other static polymorphism...

please explain me these two types of polymorphism..

better with an example
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim CakalicSenior Developer/ArchitectCommented:
Dynamic polymorphism is the "classic" form of polymorphism talked about in object-oriented development. It is obtained through the use of abstract base classes and method overriding. Remember that "method overriding" is when a subclass defines and implements a method with the identical signature as its superclass or implements an abstract method or an interface-declared method with a specific signature. With dynamic polymorphism, designers cast their designs in terms of abstract classes and the interfaces they provide and then substitute derived class objects where base class objects are expected. The actual type (class) of the object is used at run time to resolve which method should be called. An excellent example here is and its implementations. The abstract class defines a base set of services that can be expected of any reader. Some of these services are defined as abstract methods and others are concrete. Implementations such as FileReader, StringReader, and BufferedReader extend (or another of its subclasses) and provide specialized implementations of the same methods. Once one of these specialized objects is constructed, clients are able to substitute them anywhere is used and the JVM will resolve method calls to the subclass at run-time.

    // originally 'in' is a FileReader
    Reader in = new FileReader(filename);
    char[] buf = new char[100];;
    // now 'in' is a StringReader with same methods available
    in = new StringReader(new String(buf));
    for (int char =; char >= 0; char = {

I don't think a whole lot more needs to be said about this. The compiler issues invoke instructions based on the type of the 'in' object being a Reader. At run time, the JVM determines the actual class of the 'in' object and finds the read method defined by the most-derived subclass in the inheritance hierarchy for the object. That's "dynamic polymorphism" because it happens at run time.

Static polymorphism, on the other hand, is a compile time binding. In Java, the only way that it can be obtained is by method overloading. A method with the same name can have different signatures. When the compiler encounters code that calls an overloaded method, it determines the type of the object being passed as a parameter to the method based on the nearest declaration of the object reference and selects the appropriate overloaded method. Note that the method is chosen at compile time, not at run time. As such, the binding is static, not dynamic. An example here is the It defines a number of overloaded print and println methods. There is a println(String) and a println(Object). When the compiler encounters a PrintWriter.println(ref) method in your code, it determines, based on the nearest declaration of 'ref' whether or not it is a String. If it can determine that it is indeed a String then it issues bytecode to invoke the println(String) otherwise it issues bytecode to invoke the println(Object) method. This can get 'tricky' as the following code reveals:

public class StaticPolymorphism {
    PrintWriter out = new PrintWriter(System.out);

    public void printOnce(Object ref) {

    public void printTwice(String ref) {

    public static void main(String[] args) {
        String s = "Hello, world!";
        StaticPolymorphism instance = new StaticPolymorphism();

Here the call to printTwice takes a String parameter. Within the printTwice method the compiler determines that the nearest declaration of 'ref' is a String from the formal arguments of the method. So the call to out.println(ref) in printTwice calls PrintWriter.println(String). The call to printOnce from printTwice, however, might cause you some confusion. Within printOnce, will the out.println(ref) end up calling PrintWriter.println(String) or PrintWriter.println(Object)? We look at the code and can deduce that in this once scenario a String is being passed. But because printOnce declares that its parameter is an Object, the compiler cannot make the same assumption. The fact of the matter is that the type of 'ref' within printOnce is Object. Therefore the compiler statically binds to the PrintWriter.println(Object) method. The run time type of the object has no effect on which method is called. To prove it, here's the bytecode for printOnce and printTwice:

    public void printOnce(Object obj)
    //    0    0:aload_0        
    //    1    1:getfield        #12  <Field PrintWriter out>
    //    2    4:aload_1        
    //    3    5:invokevirtual   #15  <Method void PrintWriter.println(Object)>
    //    4    8:return          

    public void printTwice(String s)
    //    0    0:aload_0        
    //    1    1:getfield        #12  <Field PrintWriter out>
    //    2    4:aload_1        
    //    3    5:invokevirtual   #21  <Method void PrintWriter.println(String)>
    //    4    8:aload_0        
    //    5    9:aload_1        
    //    6   10:invokevirtual   #23  <Method void printOnce(Object)>
    //    7   13:return          

Notice that in printOnce the target of the invokevirtual instruction for out.println(ref) is method #15 in the class of the 'out' object which the comment from the class decompiler identifies as PrintWriter.println(Object). In printTwice, the target of the invokevirtual for out.println(ref) is method #21 in the class of the 'out' object which is identified as PrintWriter.println(String). The compiler determined which overloaded method to call based on the nearest declaration of the object. That's "static polymorphism" because it happens at compile time.

Hope that helps.

Best regards,
Jim Cakalic

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.