• Post Reply Bookmark Topic Watch Topic
  • New Topic

setColor() method in the Graphics class  RSS feed

 
jack white
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If abstract methods always need to be overridden then why do we
never override the setColor method (and tens of other abstract methods in the Graphics class) before using it?
Why do we simply say <<Graphics object>>.setColor(Color.Red) for instance?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search for polymorphism and you will find the answer.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, Rob, I interpret that question slightly differently.

If you go through the API both Graphics and Graphics2D are marked "abstract" and their setColor() methods are also abstract. But we get Graphics objects sent to the paintComponent() method, which can be cast to Graphics2D. So obviously the JVM has access to a class which instantiates Graphics2D, either by extending it, or by implementing all the abstract methods. And a reference to that class instance is sent to paintComponent().

. . . and welcome to JavaRanch
 
jack white
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritchie, you got exactly what I meant!
Thanks a lot for the reply.
But does that mean there are many method implementations (like setColor) in Java which are 'hidden' in JVM?
If that is the case then I am relieved - otherwise I was frantically looking for method implementations whose source code is probably unavailable(?) to the programmers.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't even want to know how many hidden classes there are:
  • Classes that have default access like java.lang.AbstractStringBuilder*)
  • Classes that are static / inner classes of other classes; there are several in java.util.Collections, and don't forget each Iterator implementation
  • Classes in one of the Sun-private packages - those that start with com.sun, sun or sunw. These packages contain the most used implementations of abstract classes like sun.awt.windows.WToolkit or sun.java2d.SunGraphics2D. Guess of which classes those are the implementations under Windows

  • The good thing is, just like with the interfaces in java.sql, you don't need to know the exact class you are using. You just need to know that it is a Graphics (or Graphics2D) and use its methods.

    * the base class of both StringBuilder and StringBuffer. Yet we are not allowed to use it... Which sucks because now I need to write two methods a lot of the time - one that takes a StringBuilder and one that takes a StringBuffer. If only AbstractStringBuilder would be public, I could just use that and don't care which one is passed.
    [ September 17, 2008: Message edited by: Rob Prime ]
     
    jack white
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Rob...I realise it now....
    Information you provied was very useful.
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Rob Prime:

    * the base class of both StringBuilder and StringBuffer. Yet we are not allowed to use it... Which sucks because now I need to write two methods a lot of the time - one that takes a StringBuilder and one that takes a StringBuffer. If only AbstractStringBuilder would be public, I could just use that and don't care which one is passed.


    Are you aware of the interfaces Appendable and CharSequence?
     
    Rob Spoor
    Sheriff
    Posts: 21135
    87
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am but Appendable is only limited to chars and CharSequences. I love the ability of StringBuilder and StringBuffer to append anything without having to manually convert the items to string. But perhaps I need to stop being lazy for once
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!