×

Loading...

NOT ALL OBJECTS ARE CREATED BY US USING new...

本文发表在 rolia.net 枫下论坛To answer your question, I first create an example:
//other code
Animal animal = new Bird();
animal.move();
//other code

We assume Animal is an abstract class
and it has an abstract method called move().

As I say animal.move(), it does not mean
JVM will use the abstract method of
the Animal class. Instead, it will find this
animal is actually a bird and use the move
method of the Bird. Put it in another way,
JVM will use an overriden version of the
move() method.

---This is the so-called late binding or dynamic binding in Java.

For the whole JVM, there is only ONE
Toolkit. This is the so-called singleton pattern. We, Application Programmers,
cannot create a Toolkit using the "new" keyword. Instead, we use the sole Toolkit
intance. Toolkit.getDefaultToolkit() returns us
a referece (or pointer) to the sole Toolkit.
Then, we assign it to ttkk.

At this stage, let me ask you a question: What
kind of Toolkit do you have? The answer is:
the Default Tookit. OK, now you try to call getImage() method. At this point, JVM will use the overriden version in the default toolkit. Again. late binding, or dynamic binding. The abstract method IS NOT used.

Come back to my animal example. I have
three animals. I let them move by calling the
move() method. The first animal is a bird, so
it will fly; the second one is a snake, so it will crawl; the third one is a frog, so it will jump.....

Hope this can help更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report