×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

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

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / hi,jabber.I have a question about abstract class of java?
    in java, the abstract class can not new an object.So can i use the abstract method in the abstract class?which way?
    the next code is run well,why?

    eg. Toolkit ttkk=Toolkit.getDefaultToolkit();
    Image img1=ttkk.getImage("abc.gif");

    in this code, getImage() is the abstract method. It seems that you can use the abstract method directly. other more, what is the role of the ttkk? certainly,it isn't the object.
    in documents of java, it seems the object of the subclass of Toolkit. is it right?

    above all.there are two way to explain the code:
    1. you can use the abstract method of abstract class.
    2.from the static method getDefaultToolkit, you got the subobject of the Toolkit?
    jabber,which one is right?
    • 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
      • thank you,jabber .i got it.