Re: Why is dynamic polymorphism so useful?
- From: failure_to@xxxxxxxxxxx
- Date: Fri, 28 Dec 2007 14:58:14 -0800 (PST)
hello
* I must first point out that I realize ( even when I first started
this thread ) that with the help of polymorphism we can get a generic
interface and thus don’t have to change a code when new subclass is
introduced!
1) Now what is most bugging me about polymorphism is the following:
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. Let me explain what I mean:
Say we have superclass A and bunch of subclasses ( all of them
override A’s method a1() ). Now somewhere in this program is a code
that calls a1() via reference variable ref_var:
* A ref_var; // here we define ref_var
* At some point in a program we assign to ref_var a reference to some
object ( this object might be of class type A or one of its
subclasses )
*then we call ref_var.a1(); // which method to actually call is
resolved at runtime
Say we start this app and while app is already running, we write a new
class called N_C.
The already running app has to be flexible enough to enable an object
of type N_C not only to be added to running app ( without even
restarting this app ), but to actually use this object.
But how do we do that? Let me explain it further:
Say this running app has an object of type B, where B is subclass of
A, and if I want ref_var to reference this object, then I do this by
writing in program code the following line:
B b = new B():
ref_var = b;
The above lines are hard coded into program. But how do we assign
ref_var a reference to object of type N_C, if we introduce this new
class to already running app --> somewhere in the program code the
following two lines would have to be written:
N_C n_c = new N_C();
ref_ver = n_c;
But problem is app is already running and we don’t want to stop this
app to write the above two new lines and yet we want this app to
somehow use this new object ( by assigning ref_var a reference to
n_c ). Now how can assign ref_var a reference to object of type N_C
without adding the above two lines into code ?
Anyway, what did you think of the answers? Make sense? Raise other
questions?
--
Mark Rafn da...@xxxxxxxxx <http://www.dagon.net/>
Perhaps I shouldn't say this, cos you went through so much trouble in
order to help me, but truth be told questions were a bit too technical
for me and even if I did somewhat understand what ( some of ) you were
saying, I didn’t understand why would arguments you provided prove
anything ( at least I didn’t find them to prove anything, but then
again, I'm not exactly ...). But I do know that in time ( when I get a
bit better in programming ) these answers will be of great value
I will give just a few examples:
1) With dynamic polymorphism, calls to actual methods are
resolved at run time. So why is that so important? Why is that
so much better than letting the compiler figure out which
version of method to call
( based on the type of object being referred by reference
variable )?
Result would still be the same ... if superclass variable
referred to child class object, then method of child class
object would be called!
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.
Why couldn’t java know at compile time what to do with rabbit method?
Perhaps because creators of Java decided compiler doesn’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?
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 don’t understand how this proves anything? Perhaps if this was done
at compile time, then internally ( I’m talking out of my arse now )
there could be another
if ( n is integer then call n.toString(Integer n) )
else ( else if n is double then call n.toString( Double n ) )
statement that enable the compiler to "call" the appropriate method.
Result would still be the same ... if superclass variable
referred to child class object, then method of child class
object would be called!
It can change between compiliation and runtime. Heck, it can
change during runtime.
What can change?
BTW - I do realize that compiler can’t resolve method calls due to
Java ability of introducing new classes to program ( without the need
to recompile a program )? 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 )!
uh
.
- Follow-Ups:
- Re: Why is dynamic polymorphism so useful?
- From: Patrick May
- Re: Why is dynamic polymorphism so useful?
- From: Lew
- Re: Why is dynamic polymorphism so useful?
- References:
- Why is dynamic polymorphism so useful?
- From: failure_to
- Re: Why is dynamic polymorphism so useful?
- From: Mark Rafn
- Re: Why is dynamic polymorphism so useful?
- From: failure_to
- Re: Why is dynamic polymorphism so useful?
- From: Mark Rafn
- Why is dynamic polymorphism so useful?
- Prev by Date: Re: Flash Movie Conversion with Java (FFMPEG)
- Next by Date: Re: Java
- Previous by thread: Re: Why is dynamic polymorphism so useful?
- Next by thread: Re: Why is dynamic polymorphism so useful?
- Index(es):
Relevant Pages
|