Posted on 2002-04-29
Medium Priority
Last Modified: 2008-03-03
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
Question by:yrrm
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
LVL 19

Accepted Solution

Jim Cakalic earned 200 total points
ID: 7014826
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 java.io.Reader and its implementations. The abstract class java.io.Reader 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 java.io.Reader (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 java.io.Reader 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 = in.read(); char >= 0; char = in.read()) {

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 java.io.PrintWriter. 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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question