Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Polymorphism and dynamic binding

Thread view: 
kaja_love160@yahoo.com - 06 Jun 2007 20:50 GMT
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
Joshua Cranmer - 06 Jun 2007 22:12 GMT
> 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
[quoted text clipped - 8 lines]
> 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).


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.