Re: Why is dynamic polymorphism so useful?



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
.



Relevant Pages

  • RE: Upgrading a web application to VS 2005
    ... after you converting a VBNET web project from VS2003 ... 1/ Where has all this code gone as the compiler can no longer find it? ... back into the app in VS 2005? ... you've already read some upgrade and migration reference on the MSDN ...
    (microsoft.public.dotnet.general)
  • using OPTIONAL third party assembly
    ... I have a 3rd party tool I'd like my application to ... but I don't want it to be *required* by the app. ... If I add a reference to the web app, I get a web.config assembly entry ... If I don't add the reference, the compiler doesn't know anything ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to convert Infix notation to postfix notation
    ... Even if we grant that, there remain others which are not matters of opinion, ... and cannot use it as a *reference*. ... Failure to do so causes common bugs. ... I do compiler stuff; we don't actually do our own compiler/libc ...
    (comp.lang.c)
  • Re: Using early-bound interface on a late-bound object
    ... > the compiler determines which interfaces to use, ... > supports the declared interface. ... >> help if someone can point me to some authoritative document or reference ... within customer shops when they mirrored this 'division' to keep their ...
    (microsoft.public.vb.general.discussion)
  • Re: Garbage Collection Eligibility and portability
    ... (in the parlance of compiler writers). ... There isn'tany way to get a given Lisp to act that way. ... that alters exactly when a variable reference to a memory location ceases holding it from collection. ... The other, which I've seen called "Aggressive garbage collection" in some articles on this behavior in C#/.Net, appears to make a memory location eligible the instant the last reference to the location is used. ...
    (comp.lang.lisp)