• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code Barn example Shape - I have a question..

 
Rory Lynch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Docherty
Bartender
Posts: 2988
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tony.

I will do that right after I have some lunch.
 
Rory Lynch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rory Lynch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

I think I'll read that a few times to let it sink in
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic