Forums Register Login

( Graphics g )

+Pie Number of slices to send: Send
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*
+Pie Number of slices to send: Send
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 ]
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 755 times.
Similar Threads
Graphics Object & Graphics Context
Logos or Pics for JPanel
Graphics Class
Abstract class Graphics
Graphics class
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:38:25.