Originally posted by: manly
Virtual method invocation (the default in Java) does what you want. It seems you need to brush up on some OOP fundamentals.
To repeat what Bloodstein said, don't implement the code he suggested. I don't see any upside to that at all.
For example:
abstract class A {
abstract void draw(Graphics g);
public int print(Graphics g, PageFormat pf, int index) {
// Some implementation that calls draw(g)
}
}
class B extends A {
void draw(Graphics g) {
// A specific implementation of drawing B into the graphics context
}
}
With virtual method invocation (aka dynamic dispatch), for an instance of B, inherited method print(...) will automagically call B.draw(g). In fact, in this sample class hierarchy, A.draw() has no implementation.
First, thanks for your replies.
I actually tried that already and got the following error:
java.lang.NullPointerException
at mapper.StarMapPanel.draw(StarMapPanel.java:170) which is: starMap.paint(g); starmap is a subclass of classB and paint is the method for painting a component
at mapper.MapperPanel.print(MapperPanel.java:263) which is: draw(g); and is in classA
at sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:1480)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1078)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:979)
at mapper.MapperPanel.actionPerformed(MapperPanel.java:219)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)
at javax.swing.AbstractButton.doClick(AbstractButton.java:289)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1109)
at javax.swing.plaf.basic.BasicMenuItemUI$MouseInputHandler.mouseReleased(BasicMenuItemUI.java:943)
at java.awt.Component.processMouseEvent(Component.java:5093)
at java.awt.Component.processEvent(Component.java:4890)
at java.awt.Container.processEvent(Container.java:1566)
at java.awt.Component.dispatchEventImpl(Component.java:3598)
at java.awt.Container.dispatchEventImpl(Container.java:1623)
at java.awt.Component.dispatchEvent(Component.java:3439)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
at java.awt.Container.dispatchEventImpl(Container.java:1609)
at java.awt.Window.dispatchEventImpl(Window.java:1585)
at java.awt.Component.dispatchEvent(Component.java:3439)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
I've never taken a class on java. I had to use it in a class, but they never taught us anything, we had to learn on our own and I just read a book, so there may be a lot of things that i am not familiar with. But i do have a question? From classA, can you call a method if its abstract?