Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Abstract Class Graphics

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Graphics class (java.awt.Graphics) is declared as an abstract class and is the superclass of class Graphics2D and DebugGraghics. The Graphics method .setColor() is also declared abstract, so I expect this method has no body and no 'behavior'-influencing function within the Graphics class. The .setColor() method is also not defined in the Graphics2D class; in fact, this method is not to be found in the Graphics2D class at all. Still, if I call the method (Graphic g; g.setColor(Color.red)), it actually works!!?? That would mean that the .setColor() method of the Graphics class does have a body (is defined) and has a function. Can someone tell me why/how this works? Thanks.
 
Marshal
Posts: 3763
536
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Legué wrote:.. if I call the method (Graphic g; g.setColor(Color.red)), it actually works!!?? ..


Can you create a SSCCE (that's a link) and post it here?
 
Sheriff
Posts: 26966
84
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you get an object whose type appears to be "Graphics", the actual class of that object will be some subclass of Graphics. Of course this subclass cannot be abstract, since you have an object of that subclass. So that subclass must have an implementation of setColor(Color). That's just how the "abstract" keyword works, it's not specific to Swing classes.

You're probably expecting that the subclass in question will be something whose documentation you can see in the API. But that isn't the case; Swing has its own internal classes for two reasons: first, there are different classes for different operating environments (Windows, Unix, etc), and second, there are different classes for different look-and-feels. You can find out the name of an object's class by calling its getClass() method, so why don't you try that on your "Graphics" object and see what you find out?
 
Paul Clapham
Sheriff
Posts: 26966
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Marshal
Posts: 74770
336
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:When you get an object whose type appears to be "Graphics", the actual class of that object will be some subclass of Graphics. . . .

I think it is an instance of Graphics2D (or a subtype), so you can cast the Graphics instance to 2D

And welcome again
 
Richard Legué
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the helpful reactions sofar. I know for sure that the 'g'-object in question appears to be an instance of the Graphics class, but is in fact a Graphics2D object. But the Graphics2D class is (in the API) also abstract, just like its superclass Graphics. So I'm still puzzled:
1. how can I have an object of an abstract class;
2. that uses a method (setColor) which - according to its API) - is an abstract method in the superclass Graphics (so has no body) and is not implemented in Graphics2D.
Call me thick, but I'm trying to understand how this works. Cheers!
 
Rancher
Posts: 3253
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but is in fact a Graphics2D object.



No, it is a class that extends Graphics2D, this is why you can cast it to a Graphics2D.

Paul showed you how to get the actual class name. Reread the suggestion.
 
Richard Legué
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I will. Thanks!
reply
    Bookmark Topic Watch Topic
  • New Topic