*
The moose likes Beginning Java and the fly likes Understanding abstract methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Understanding abstract methods" Watch "Understanding abstract methods" New topic
Author

Understanding abstract methods

Billy Jorgensen
Greenhorn

Joined: Jan 07, 2008
Posts: 1
Hi everybody,

As I was reviewing an example used in the Head First Java book (second edition), bottom of page 367, I went to the API to look up a method, setPaint(), used in the example and noticed that all of the methods in the class (java.awt.Graphics2D) that setPaint() belongs to are abstract.

My question is, how do these methods work if they are all abstract? In the example in the book, there is this code:



FYI, the setPaint() method takes a Paint argument, and the example in the book creates a GradientPaint object (represented by the gradient variable), a subclass of the Paint class, so it is polymorphically OK for setPaint() method to take the gradient variable argument. This makes sense to me.

What I don't understand is why we're using the Graphics2D setPaint() method if it is abstract? An abstract method has no code implementation, which would mean the abstract setPaint() method in the Graphics2D class has no code implementation, which makes me wonder how it can set the Graphics2D class' Paint attribute in the first place? If we wanted to set a Paint attribute, wouldn't we have to create a subclass of Graphics2D that provides method implementation code for setPaint(), thereby providing a way to set a Paint attribute? Is there some unique functionality going on in this class (and other similar classes) that I'm not aware of?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

It sounds like you haven't understood yet exactly how polymorphism works.

The implementation of the abstract method is in a subclass that you don't see. Consider this Shape class and its subclass Circle:

Now you can create a variable of type Shape that refers to a Circle object, and you can call draw() on s:

Note that you can call draw() on s, even though s is a Shape and draw() is abstract in class Shape.

This works the same way in your example. The Graphics2D object that you have really refers to an instance of a non-abstract subclass of Graphics2D. You don't need to know exactly what that class is - you only need to know that all methods of class Graphics2D can be called on it.

If you'd really like to know what kind of object g2d refers to, you can print out its class name like this:

System.out.println(g2d.getClass().getName());


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Michael Breuer
Greenhorn

Joined: Nov 18, 2007
Posts: 7
Hi,
I have not that book but in your code example there is an Object g which is casted to Graphics2D. So if g is a Subclass of Graphics2D I think the Graphics2D-Methods can be used. Note the g-Object is not a Graphics2D-Object which has the abstract Methods. It is not possible to instanciate Objects of abstract classes. If you create an Object all Methods are implemented. g has the implemented Methods which are abstract in Graphics2D and the Methods can be called on this Object.
 
wood burning stoves
 
subject: Understanding abstract methods
 
Similar Threads
JButton Custom Background
Code Barn example Shape - I have a question..
inheritance
casting
abstract class