Re: Why is dynamic polymorphism so useful?
failure_to@yahoo.co.uk wrote:
I also realize, that with polymorphism we can run a program and while
program is already running, we can introduce a totally new class ( let
us call this new class N_C ) and thanx to polymorphism this program
may operate on instances of N_C without the need to recompile or even
without the need to restart this program.
But how to write a code for this program in such flexible way that
program will KNOW that new class is present and operate on it is
beyond me.
Java has basically three ways to accomplish this: classloaders, reflection and
the debugger API.
All three are advanced topics. Let me give just the briefest summary.
Classloaders can be directed to reach out to a known source and load the
bytecode for a class while the program is running.
Reflection lets a program retrieve instances from classes not known until run
time, perhaps ones that have been retrieved through a custom classloader.
The debugger API has hooks that let a program substitute different class
versions for each other while the program is running.
Didn't catch who said:
You can get a more accurate match. If a reference is an Object
but the object itself is a Rabbit, you can get the specialised
Rabbit method. Java does not know to use the specialised Rabbit
method at compile time.
failure_to@yahoo.co.uk wrote:
Why couldn2"t java know at compile time what to do with rabbit method?
That's "Java", not "java".
This was explained a few times in this thread. It's not what "Java" knows,
it's what the variable type is, that determines what methods the variable can
call. Remember from upthread that variables have compile-time type, objects
have run-time type.
Perhaps because creators of Java decided compiler doesn2"t have to know
that, since, afteral, they were gonna implement dynamic binding, or is
it in general not possible for compiler to know that, even if creators
of Java wanted for compiler to know that?
No need to get religious - the creators of Java have nothing to do with this.
Java is defined by a set of rules, and those rules determine the behavior.
The key is to remember that variables have only compile-time type - objects
have run-time type. The variable knows how to ask for something in the
compile-time type, e.g., "x.run()" for a variable "x" of type "Runnable".
Since there is no information in the compiler about the run-time type of the
actual object, it is only valid to say that "x" calls its "run()" method. The
actual object at run time will pick up the request for a "run()" and simply
use its own method to do it. That's all.
Please explain how the compiler can figure out which class'
method to call in
Number n = (Math.random() < 0.5)
? new Integer(42) : new Double(42.0);
String s = n.toString(); // Integer's toString, or Double's?
I don2"t understand how this proves anything? Perhaps if this was done
at compile time, then internally ( I2"m talking out of my arse now )
there could be another
The whole point is that the compiler only knows that the variable 'n' is a
'Number'. No more is needed, actually.
At run time, the object will pick up the call to toString(), and use its own
version of toString() to fulfill the call, is all.
if ( n is integer then call n.toString(Integer n) )
else ( else if n is double then call n.toString( Double n ) )
No need for that when the object itself already knows how to toString()
itself. It's called separation of concerns, and encapsulation. The calling
method doesn't need to care about the details - it just trusts the object to
call its own method on its own.
statement that enable the compiler to "call" the appropriate method.
Compilers don't call methods, objects do. Compilers just tell the object
which method to call.
Result would still be the same ... if superclass variable
referred to child class object, then method of child class
object would be called!
How would a compiler know what object is in play in the future?
It can change between compiliation and runtime. Heck, it can
change during runtime.
What can change?
The object that is asked to perform the method can change.
BTW - I do realize that compiler can2"t resolve method calls due to
Java ability of introducing new classes to program ( without the need
to recompile a program )8WV thus compiler has no way of knowing what
classes may be added to program in the future ( the questions I
originally asked ignored on purpose this Java ability )!
This has nothing to do with polymorphism. Polymorphism is simply that the
object decides for itself how to execute a method.
--
Lew