• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

( Graphics g )

 
Leon Clark
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone understand why java.awt.Graphics, as an Abstract class, can be instantiated in the Applet class extention "public void paint" method?

A line like: Graphics g = new Graphics(); // Error! Abstract Class.

So why can the paint method take a Graphics "Object" as a parameter if Graphics itself is an Abstract class?

Thanks
*Leon*
 
Joe Ess
Bartender
Posts: 9337
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Leon.
The Graphics instance used by paint will be a concrete subclass of Graphics. That way you know what methods and members you have to work with but you don't know all the details about how they work (one of the foundations of Object Oriented Programming).
We see this all over the Java language. The Collections classes in java.util.
java.util.Collections has a bunch of methods which work on List like rotate, shuffle sort and so on. List is an interface. We can't create an instance but we can pass it a concrete implementation of List, like ArrayList or LinkedList. Collections doesn't care what the implementation of the list is but it can provide valuable functionality to any object which implements the List interface. In the same way, you can write your paint method to deal with any graphic display without really caring how the display is implemented.
[ December 09, 2005: Message edited by: Joe Ess ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic