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 Beginning Java and the fly likes Code Barn example Shape - I have a question.. 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 » Beginning Java
Bookmark "Code Barn example Shape - I have a question.." Watch "Code Barn example Shape - I have a question.." New topic
Author

Code Barn example Shape - I have a question..

Rory Lynch
Ranch Hand

Joined: Aug 03, 2007
Posts: 95
Hi there everyoe.

I am new to Java Ranch and new to Java and I am studying hard to try and get the SCJP cert.

In the Code Barn there is an example called Shape which is a demo of Polymorphism for beginners.

It uses a method which it invokes using;
g.fillRect(x, y, wide, high, color)

where g refernces the Graphics class from the java.awt package.

My question is "If the filRect method is an abstract method, which it appears to be from the Java API docs, how come I can use that method without writing the code for it?"

When I looked up the method in the Java API I did not expect it to be abstract.


I wish that for just one time, you could stand inside my shoes.<br />You'd know what a drag it is to see you.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2249
    
  47
Most of the methods in Graphics and Graphics2D are in fact abstract. The thing is you don't actually get passed a Graphics Object but rather you get passed an object which is a subclass of the Graphics class, one which has implemented all of the methods. If you want to see what you are actually being passed add the following line to your paint() method (where g is the graphics object being passed to the paint method).

Rory Lynch
Ranch Hand

Joined: Aug 03, 2007
Posts: 95
Thanks Tony.

I will do that right after I have some lunch.
Rory Lynch
Ranch Hand

Joined: Aug 03, 2007
Posts: 95
So I added the code you suggested and I got;

Graphics class = sun.java2d.SunGraphics2D

which explains why the method is marked abstract in the Graphics class so thanks for answering my question, but...

where is SunGraphics2D in the Java API doc?

How did the compiler know which class to use when I told it to use the Graphics class in the awt package?

How would I have known that I can invoke the abstract method if reading the API doc tells me it is abstract?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
where is SunGraphics2D in the Java API doc?

Nowhere Any sun.* and com.sun.* methods are not part of the Java API, and thus not documented. But that's OK, because the only methods of that object your code should be using are the ones defined in Graphics. A different JVM may use a different class (e.g. I think that the Linux or IBM JVMs don't have classes that are in the sun.* hierarchy).

How did the compiler know which class to use when I told it to use the Graphics class in the awt package?
The compiler didn't know (and doesn't need to know - it only cares about which methods Graphics has). The important thing is that the JVM knows how to create objects that extend Graphics, though.

This is similar to working with interfaces. Whenever a method uses an interface, the compiler doesn't know which class the object being used will have. It only knows that the object implements the interface, so the methods in that interface are accessible by the code that follows.

How would I have known that I can invoke the abstract method if reading the API doc tells me it is abstract?

It's implied by the fact that you're dealing with an object. While classes can have abstract methods, actual existing objects can't. So you know that whatever methods are defined -abstract or not- will be present and accessible.
[ August 11, 2007: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Rory Lynch
Ranch Hand

Joined: Aug 03, 2007
Posts: 95
Thank you.

I think I'll read that a few times to let it sink in
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code Barn example Shape - I have a question..