Polymorphism (satic and dynamic binding)

Static Binding:

           

Static binding is deciding at compile time which methods to invoke.

The method gets invoked is determined by the type of the reference.

 

In Java, static methods are statically bound.

Also class member variable is applicable for static binding because,

In Java does not support polymorphic behavior for member variables.

 

 

 

For example,

 

public class Animal

{

            public String type = “mammal”;

           

            public void show()

            {

                        System.out.println(“The animal is a: “ + type);

            }

}

 

 

public class Dog extends Animal

{

            public String type;  //same member variable name as in base class

           

            public Dog(String type)

            {

                        this.type = type;

            }

 

            public void show()  //same method signature as in base class

            {

                        System.out.println(“The dog is a: “ + type);

            }

}

 

 

 

public class DemoStaticBinding

{

            public static void main(String[] args)

            {

                        Animal doggie = new Dog(“daschund”);

                        doggie.show(); // “The dog is a: daschund”  (dynamic binding)

                        System.out.println(“The type is: “ + doggie.type); //”The type is: mammal” (static binding)

            }

}

 

 

 

Dynamic Binding:

           

Dynamic binding is deciding at run time which methods to invoke.

 

 

Here I explain the concepts,

 

public class Shape {

            private String name;

 

            public Shape(String aName) {

                        name = aName;

            }

 

            public String getName() {

                        return name;

            }

 

            public float calculateArea() {

                        return 0.0f;

            }

 

            public static void main(String argv[]) {

            Circle c = new Circle(“Circle C”);

            Square s = new Square(“Square S”);

            Shape shapeArray[] = {c, s};

            for (int i=0; i<shapeArray.length; i++) {

                        System.out.println(“The area of “ + shapeArray[i].getName()

                                                + ” is “ + shapeArray[i].calculateArea()+” sq. cm.\n”);

            }

            }

}

public class Circle extends Shape {

            private int radius;

 

             Circle(String aName) {

                        super(aName);

                        radius = 3;

            }

 

            public float calculateArea() {

                        float area;

                        area = (float) (3.14 * radius * radius);

                        return area;

            }

}

 

 

class Square extends Shape {

            private int side;

 

             Square(String aName) {

                        super(aName);

                        side = 3;

            }

 

            public float calculateArea() {

                        int area;

                        area = side * side;

                        return area;

            }

}

 

 

Based on the output from the code, it is clear that the appropriate method for

responding to the choice in shapeArray has been used:

The area of Circle C is 28.26 sq. cm.

The area of Square S is 9 sq. cm.

 

The method has been selected based on the class of the shape referenced in

shapeArray at run-time. This is only possible in programming languages that support

dynamic binding. With dynamic binding, the variable shapeArray[i] is bound

to an object method at run time when the class definition of the shape referenced is

known.

 

Note:

            Static binding is limited and may lead to difficulty software maintenance.

On the other hand dynamic binding is very flexibility and easy to maintain.

 

 

4 thoughts on “Polymorphism (satic and dynamic binding)

Leave a comment