Public Group

# Java, keyword super, about the execution path, don't understand Step Into

This topic is 2681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

First, we have these two classes.

public class Superclass { public void printMethod() { System.out.println("Printed in Superclass."); } } 

public class Subclass extends Superclass { public void printMethod() { //overrides printMethod in Superclass super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); }}

In Eclipse, I tried using the debugger "Step Into", but instead of telling me where its execution path is going to go, it jumps from one method on towards the outer part of the source code, thus giving me the error that it can't find the source code of the method which is using the keyword "super".

Whatever the case, the main question is, for the code above, am I correct if I say the execution path in the main() method goes like this?

 EXECUTION START Subclass s = new Subclass(); s.printMethod(); super.printMethod(); System.out.println("Printed in SuperClass."); System.out.println("Printed in Subclass."); return void; EXECUTION END 

##### Share on other sites
No, this is more correct:

 EXECUTION START INSIDE MAIN Subclass s = new Subclass(); INSIDE SUBCLASS CONSTRUCTOR super.printMethod(); INSIDE PRINT METHOD FOR SUPERCLASS System.out.println("Printed in SuperClass."); INSIDE SUBCLASS CONSTRUCTOR s.printMethod(); INSIDE PRINT METHOD FOR SUBCLASS System.out.println("Printed in Subclass."); INSIDE MAIN return void; EXECUTION END 

In which case the output should be:

Printed in SuperClass. Printed in Subclass. 

Also I don't know anything about the Eclipse IDE but it seems something is wrong.

##### Share on other sites
In Eclipse, I tried using the debugger "Step Into", but instead of telling me where its execution path is going to go, it jumps from one method on towards the outer part of the source code, thus giving me the error that it can't find the source code of the method which is using the keyword "super".[/quote]

I guess you tried to step into System.out.println and Eclipse failed to do it.

##### Share on other sites
Thanks! Now I can tell that the keyword "super" can invoke the superclass' methods. The only thing I can't understand is why there are information that "super" invokes a superclass' constructor method instead of directly calling the function that was supposed to be called.

In Eclipse, I tried using the debugger "Step Into", but instead of telling me where its execution path is going to go, it jumps from one method on towards the outer part of the source code, thus giving me the error that it can't find the source code of the method which is using the keyword "super".

I guess you tried to step into System.out.println and Eclipse failed to do it.

[/quote]

Thanks!

##### Share on other sites

The only thing I can't understand is why there are information that "super" invokes a superclass' constructor method instead of directly calling the function that was supposed to be called.[/quote]
I can't quite parse this - can you clarify your question?

The "super" keyword is used for two things. One is to specify arguments to a super class constructor, and another is to call the implementation of a method in the super class. The former is done by having as the first line of any constructor "super( /* args */ );". The latter is as has been shown in this thread.

You seem to think that these things might be mutually exclusive. They are quite distinct. If you need to extend a certain class, you might need to construct the superclass portion with a specific set of arguments. You'll need to do this regardless of whether you plan on calling superclass methods later.

Well designed classes minimise the need to explicitly specify superclass function implementations. They will provide "hooks" that you must override, and functionality that the superclass encapsulates may be marked final. Or they might be split into two, the functions which subclasses would have is moved to an interface, and the superclass is declared final.

##### Share on other sites

The "super" keyword is used for two things. One is to specify arguments to a super class constructor, and another is to call the implementation of a method in the super class. The former is done by having as the first line of any constructor "super( /* args */ );". The latter is as has been shown in this thread.

You seem to think that these things might be mutually exclusive. They are quite distinct. If you need to extend a certain class, you might need to construct the superclass portion with a specific set of arguments. You'll need to do this regardless of whether you plan on calling superclass methods later.

Well designed classes minimise the need to explicitly specify superclass function implementations. They will provide "hooks" that you must override, and functionality that the superclass encapsulates may be marked final. Or they might be split into two, the functions which subclasses would have is moved to an interface, and the superclass is declared final.

In short, in a subclass of a superclass, "super.SomeMethod()" calls a method "SomeMethod()" in the superclass, while "super()" calls the constructor of the superclass. I can also use a lot of similar "super.OtherMethods()" calling conventions if I were to call some methods within a superclass.

I guess it makes much more sense now. The only thing left of it is to clarify the question I was referring.

In a class javax.swing.JPanel, I need to extend a class MyClass from JPanel, then invoke a method overload "super.paintComponent(Graphics g)". Is "paintComponent(Graphics g)" a constructor to a method in class JPanel? Shown here a portion of an example code snippet:

 package Homework; import java.awt.Graphics; import java.awt.Color; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; public class GUI_Problem_1 extends JPanel { public static void main(String arg[]) { GUI_Problem_1 thing = new GUI_Problem_1(); JFrame app = new JFrame(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.add(thing); app.setSize(150, 150); app.setVisible(true); return; } public void paintComponent(Graphics g) { super.paintComponent(g); Random r = new Random(); int firstColor = 0; int secondColor = 0; int selectColor = 0; do { firstColor = r.nextInt(5); secondColor = r.nextInt(5); } while (firstColor == secondColor); for (int i = 0; i < 5; i++) { if (selectColor == secondColor) selectColor = firstColor; else selectColor = secondColor; g.setColor(switchColor(selectColor)); g.fillOval(15 + 10 * i, 5 + 10 * i, 100 - 20 * i , 100 - 20 * i); } } public Color switchColor(int selection) { Color myColor; switch (selection) { case 0: myColor = Color.red; break; case 1: myColor = Color.blue; break; case 2: myColor = Color.green; break; case 3: myColor = Color.yellow; break; case 4: myColor = Color.magenta; break; case 5: myColor = Color.cyan; break; default: myColor = Color.gray; break; } return myColor; } } 

The "super.paintComponent(g)" looks like it's passing a Graphic variable (empty one) to the superclass' paintComponent() method, more overloading a superclass from a subclass. This is something I never see how it's possible in C/C++. This came from Deitel How to Program Java, 7th Edition.

##### Share on other sites

The "super.paintComponent(g)" looks like it's passing a Graphic variable (empty one) to the superclass' paintComponent() method, more overloading a superclass from a subclass. This is something I never see how it's possible in C/C++. This came from Deitel How to Program Java, 7th Edition.

You already answered your question earlier in the post: "[...] and another is to call the implementation of a method in the super class". This simply calls the implementation of paintComponent of JPanel, followed by some other code. However, the Graphics variable is not empty. paintCompoment is called by Swing when the component needs to draw itself (whether on screen, to a printer or something different does not matter) and Swing ensures you will have a valid Graphics instance for the task.

Also, this is something that is extremely common in C++ as well using the syntax "MySuperClass::myMemberFunction(...args...)".

##### Share on other sites

[quote name='tom_mai78101' timestamp='1303130266' post='4799851']
The "super.paintComponent(g)" looks like it's passing a Graphic variable (empty one) to the superclass' paintComponent() method, more overloading a superclass from a subclass. This is something I never see how it's possible in C/C++. This came from Deitel How to Program Java, 7th Edition.

You already answered your question earlier in the post: "[...] and another is to call the implementation of a method in the super class". This simply calls the implementation of paintComponent of JPanel, followed by some other code. However, the Graphics variable is not empty. paintCompoment is called by Swing when the component needs to draw itself (whether on screen, to a printer or something different does not matter) and Swing ensures you will have a valid Graphics instance for the task.

Also, this is something that is extremely common in C++ as well using the syntax "MySuperClass::myMemberFunction(...args...)".
[/quote]

That quote is questioning how it's possible an empty Graphics class variable can be used to invoke drawing methods. If we could think of it as an entrance and a keylock, we should first instantiate the variable g to "new Graphics()", but I don't see how it's working like that. I could think that paintComponent() is a class placed within a class, and that "Graphics g" is passed through with no data, and gets initialized within the super.paintComponent() constructor.

Nested class... Without the ability to read the originial Java API class source files, unlike C/C++ header files, I couldn't be able to understand how it works, hence the questioning quote.

##### Share on other sites
As I said, the Graphics instance is not empty. It's the caller's responsibility to pass this method a valid Graphics object. The caller will usually be something like the Swing event handler for processing paint events. paintComponent is not a class, it is a method of JPanel (actually of JComponent which is a superclass of JPanel, but let's not get offtrack here).

The source to the Java API is available in the src.zip in your JDK directory, but I would really advice you to take several steps backs and work on simpler Java stuff first, because you really seem to be lacking several of the basics (and are very confused with several others) and the internals of Swing is not the place to learn them.
Edit: On a sidenote, I would also advice you to get away from the "must read the code" meme. Especially the Java API is exceedingly well documented and there is no magic in that code. The standard API is just a big chore which has been already done for you, there is nothing in there you cannot learn far easier outside of that code.

##### Share on other sites
Thanks for the advice, even though the book itself speaks of Swing in Chapter 3. Thanks to all.

1. 1
2. 2
JoeJ
20
3. 3
frob
18
4. 4
5. 5

• 10
• 10
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632200
• Total Posts
3004741

×