• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java Question... Help!!

kyu614

Member
Hi, I was wondering if there is anyone for a class to access methods from another class that extends it. I have class B, which extends class A and I need to call a method in class B from class A.

More specifically. I need to print the contents of a component and I need to call the draw method so it can render the graphics before i can print them. I have the parent class A (which implements Printable) and want to make a generalized print menu so i dont have to put in a print method in each of the classes that extend class A.

Does anyone have any other suggestions? I am using the following method to print "public int print(Graphics g, PageFormat pf, int index)"
If any of this is confusing, I will try to clarify what i can if you ask.

Thanks in advance.
 
Ok, if class B extends class A, there is no way class A is gonna be able to access methods of class B. A superclass can't access methods of a subclass....

but there is a way around it....note this is not good programming...

So you have 2 classes, classA and classB as follows:

public class classA {

classB b;

public classA (classB b) {
this.b = b;
}

}

public class classB extends classA {

public classB () {
super(this);
}

}

Now, if you instantiate classB, classA automatically has a reference to classB. Hence, you can call any method of class B in class A. Again, this violates the object-oriented programming principles of superclass/subclass relationships...


As for your specific case, I'm not sure what you're trying to do....if u wanna make a generalised print menu, then put it in the superclass.
 
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.
 
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?
 
Originally posted by: kyu614

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?
Yes, you definitely can call an abstract method in a concrete method implementation.

A good introductory Java book is Thinking in Java, which you can download from http://www.bruceeckel.com.

I can't really address the NullPointerException; it's happening deep inside the Swing implementation.
 
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)


A NullPointerException is a runtime error and not a compilation error. This means you have the proper access to the classes and they can see each other's methods.
However, I'll bet you will find that when you create the various object here you have totally different objects with the same name. In the example above look at the
line of code inside your StarMapPanel at line 170. It will most likely be referencing an object that is null.
 
Thanks for the link manly, now I have another source of reference.


Originally posted by: Softballslug
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)


A NullPointerException is a runtime error and not a compilation error. This means you have the proper access to the classes and they can see each other's methods.
However, I'll bet you will find that when you create the various object here you have totally different objects with the same name. In the example above look at the
line of code inside your StarMapPanel at line 170. It will most likely be referencing an object that is null.


Softballslug, thanks a lot, you solved my problem. It was a null object which i fixed. Now the problem is that the graphic does not fully get printed. I will do some testing to see how it goes, but do you guys know offhand what gets printed? because the graphic that was printed didnt start from the edges.

Again, thanks for your help.
 
Inside the paint methods I believe there are some parameters that you can set so that the edges will get printed... Best of luck and glad I could help!
 
Back
Top