This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Applets and the fly likes Abstract methods behaving like defined methods. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Abstract methods behaving like defined methods." Watch "Abstract methods behaving like defined methods." New topic
Author

Abstract methods behaving like defined methods.

Ste Davies
Greenhorn

Joined: Oct 26, 2010
Posts: 19
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

Joined: Apr 06, 2010
Posts: 4369
    
    8

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

Joined: Oct 26, 2010
Posts: 19
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

Joined: Apr 06, 2010
Posts: 4369
    
    8

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

Joined: Oct 26, 2010
Posts: 19
That's great, thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract methods behaving like defined methods.