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
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