If
I ask someone to put together a top-ten list of the most important working
underlying principles for an object-oriented software development application,
most likely polymorphism will make it at the top of list. Polymorphism is an
extremely important principle of object-oriented software development.
To
truly understand polymorphism, we need to explore one of the blueprints upon
which polymorphism is actually built: binding. Think of binding as
this piece that makes polymorphism bits.
But
what do I actually mean by binding? Binding, if you like, is a commitment(decision)
to execute a certain and particular piece of code, and that decision will be
either made by the compiler or by the interpreter
machine, during the execution of the code. When I say binding, I'm binding
method invocation (A particular method definition). Now consider, the two types
of binding in JAVA:
1) Early binding: If binding occurs by the compiler, then we talk about and refer to it as early binding (In other words, I had to do that binding before even executing that piece of code.)
Example:
Consider
overloading a method, when you overload a method, you have the same
method name but different set of parameters, consider the example below:
int sum(int a , int b) {
return a + b;
}
int sum(int a , int b, int c) {
return a + b + c;
}
Let's
invoke this piece of code by calling: sum(1,2). Which one of the
two versions you think will be executed? The first one. Why? Because you can
count the number of parameters, identify the version that will be activated and
be put in practice. So I'm here invoking sum(1,2) and I know which version will
be called, but I need to need to link/connect that statement in appropriate
method definition. Who would do the linking in your thinking? Can the compiler do
it? Yes. The compiler actually will do the selection. So when it comes to
calling overloaded methods, the compiler can make a commitment to execute a
particular piece of code. The compiler is smart and can simply count the number
of parameters to determine which version of the method should be executed or
invoked actually. When dealing with overloaded methods, compiler selects the
appropriate methods, this method selection strategy or procedure is called early
binding. EARLY BINDING is called when handling OVERLOADED methods.
2) Late
Binding: If binding occurs by the interpreter,
then talk about and refer to it as call it late binding. Another
name for late binding is dynamic binding. Late binding is used when a
polymorphic reference is used. A polymorphic reference fits the definition of
polymorphism itself. The meaning of the polymorphism itself actually means
having many shapes or having many forms, incarnations. So a polymorphic
reference will fit that definition and eventually it will embody that
definition in the sense that a polymorphic reference is an object
reference variable.
Example:
Consider
overriding a method, when you override a method, you have a
reference pointed twice, consider the example below:
class Shape{
public void draw(){
System.out.println(“Draw a shape.”);
}
}
class Circle extends Shape{
public void draw(){
System.out.println(“Draw a circle.”);
}
}
class Test{
public static void main(String[] args){
Shape sh;
sh=new Circle(); //That's a polymorphic reference
sh.move(); //Print: "Draw a shape."
sh=new Shape();
sh.move(); //Print: "Draw a circle"
}
}
It should be noted that in the
first call to draw(), the reference type is Shape and the object being
referenced is Circle. So, when a call to draw() is made, Java waits until
runtime to determine which object is actually being pointed to by the
reference. In this case, the object is of the class Circle. So, the draw() method of Circle class will be called. In
the second call to draw(), the object is of the class Shape. So, the draw() method of Shape will be called. Because we
used a polymorphic reference to call a method, late binding takes place. Let me
rephrase this: I will have to wait until the program runs before I will be able
to determine which version of the method will be used. Interpreter will do the
decision why? Because in this case the compiler cannot do it.it doesn't have
the appropriate knowledge to make the appropriate decision, so it will be up to
the interpreter to identify the method that should be executed. Late binding is
less efficient then early binding because it involves making a decision during
the execution of your code that will harm and degrade the performance of your
programs in terms of running: amount of time to complete a task. There is a
word called overhead associated with late binding: late binding is acceptable
because of the flexibility offered by polymorphic references. Flexibility is
the main key. This is our main target.
Keep In Mind:
Early binding or early polymorphism in Java is achieved by method overloading. Remember example 1.
Later binding or later polymorphism in Java is achieved by method overriding. Remember example 2.
Early binding or early polymorphism in Java is achieved by method overloading. Remember example 1.
Later binding or later polymorphism in Java is achieved by method overriding. Remember example 2.
No comments :
Post a Comment