• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Who implements drawString()

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

We saw many programs, where drawString() is called to print any message on the applet window area. But drawString() is an abstract method of Graphics class. I never saw any code, where drawString() is implemented. My question is, who implements the drawString()? If any one provides the implementation for this method either by extending Graphics class, how could we use drawString() in our applet , without accessing that particular class. Please clarify.

Thanks
Chandra
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever the paint method is called, an instance of a concrete subclass of Graphics that implements its abstract methods is sent to the method.

You can see it if you run this example.


[ February 28, 2007: Message edited by: Keith Lynn ]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Keith,

I can understand that sun.java2D.Graphics2D is the class that provides an implementation to the abstract method. But I'm a bit confused here. We neither instantiated any object of that concrete class nor we have a reference to one of its objects. So behind the scenes, how is drawString() of the concrete class is invoked. If my understanding is correct, since we have the reference of Graphics class in our paint method, it can invoke a method on its subclass. But for that don't we need to have an object to be available. Please let me know , if my understanding is correct.

Thanks
Chandra
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what you're saying, exactly. Your paint() method takes an argument of type "Graphics". When your paint() method is called, the JVM passes you an instance of some subclass of Graphics which implements all the abstract methods. You call the methods on this object.

The notion of abstract methods being implemented in subclasses is part of the idea of polymorphism. It sounds like you might not be clear on this concept. Try reading this entertaining story to help you firm up your understanding.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We neither instantiated any object of that concrete class nor we have a reference to one of its objects.


The JVM instantiates many objects behind the back of your code. E.g. the applet itself is an object, yet your code never instantiates it.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to every one. Now I understood the "Behind the scenes" scenario.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic