Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract methods behaving like defined methods.

 
Ste Davies
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm just getting started with Applets and I'm using:
http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/applet.html
which gives code for a simple applet. It uses abstract methods in the Graphics class which do useful things even
though they have no functionality. For example, the drawString(text, 15, 25); method really
does place text at the specified position; yet when I look in the Graphics class, drawString
has no code to do this. So I'm left wondering where the code might be to carry out these operations?
Any help is greatly appreciated.

Ste.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's happening is that the Graphics object being passed into the paint() and similar methods isn't simply an instance of the Graphics class. It's an instance of a concrete subclass that extends Graphics, and provides implementations for all these methods.

Because of the way polymorphism works, you don't have to worry about this. All you need to know is that it IS-A Graphics object, and so you can use all the methods in the Graphics class. But if you want to see what the actual class is, try outputing the value of g.getClass().getName() somewhere.
 
Ste Davies
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew. I found the methods in sun.java2d.SunGraphics2D. So should we assume that all abstract java methods are complete somewhere ? Or is it best to have a look for the complete class before using it?
 
Matthew Brown
Bartender
Posts: 4565
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not possible to have a non-abstract class with abstract methods, and it's not possible to instantiate an abstract class. So once you've got an object then you know it must contain an implementation of any abstract methods declared in superclasses. And it should be safe to assume that they'll behave in the way that method specifies. So you shouldn't need to worry about the particular implementing class, unless it's just out of interest.
 
Ste Davies
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's great, thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic