That isn't an error because, as your say, g
is really a Graphics2D
object instantiated by system (keep in ming Graphics2D
is a subclass of Graphics
).
When you do:
Sub sb = (Sub) new Super();
you are explicity creating a new object of the superclass. However, when you do:
Graphics2D g2d = (Graphics2D)g;
you are not creating new Graphics2D object, but casting an existing object which is already a Graphics2D
instance.
In the case of swing rendering, the object to perform graphics operations is a Graphics2D
object, but paintComponent()
receive a Graphics
object for backward compatibility.