• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code Barn example Shape - I have a question..

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.

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