Polymorphism and dynamic binding

K

kaja_love160

hiya


1)

First I must point out that I do understand the concept of run-time
polymorphism:

Since parent reference variable can reference a child class object,
java uses this fact to resolve calls to overriden methods at run time
( this is achieved with Dynamic binding, which allows users to execute
the most recent versions of software without re-compilation.


But I'm not sure how to implement polymorphism into code in order to
enable adding new classes without the need for re-compilation --> Say
I write a program with class A as parent and class B as a child:


public class A {

public int number() {
...
}
}


public class B extends A {

public int number() {
...
}
}


and compile it into bytecode. Suppose we add a new class ( call it
class C ) into library and then run the program ( without re-compiling
it ). Now I assume that if a program is written right, then we can
create an object called AHA of class C ( even if at time when program
was written class C didn't even yet exist ) without changing the
source code of a program?

can you show me an example of code so that we could ( using the
classes I declared in the above code ) call method of class C without
changing the program's source code ( can this program be as simple as
possible since I'm new at programming )?


public class C extends B {

public int number() {
...
}
}





2)

Also, is it possible to start a program, and then, while this program
is already running, add a new class to library and somehow use this
new class in an already running program ( without restarting the
program )?


thank you
 
J

Joshua Cranmer

But I'm not sure how to implement polymorphism into code in order to
enable adding new classes without the need for re-compilation --> Say
I write a program with class A as parent and class B as a child:

[snip]

and compile it into bytecode. Suppose we add a new class ( call it
class C ) into library and then run the program ( without re-compiling
it ). Now I assume that if a program is written right, then we can
create an object called AHA of class C ( even if at time when program
was written class C didn't even yet exist ) without changing the
source code of a program?

Not really. Polymorphism is limited to the method dispatch; using any
constructor or static method (i.e., methods invoked non-virtually)
requires the bytecode to work properly.

That said, it can be done via Java's Reflection, albeit less prettily.
can you show me an example of code so that we could ( using the
classes I declared in the above code ) call method of class C without
changing the program's source code ( can this program be as simple as
possible since I'm new at programming )?

public void test(B object) {
object.number();
}

Calling test with an argument of type C will invoke the method.
Also, is it possible to start a program, and then, while this program
is already running, add a new class to library and somehow use this
new class in an already running program ( without restarting the
program )?

Look into java.lang.ClassLoader and friends (i.e., reflection again).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top