Sign in to follow this  
tom_mai78101

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

Recommended Posts

[url="http://download.oracle.com/javase/tutorial/java/IandI/super.html"]From this page here.[/url] This is an article about using the keyword "super"
First, we have these two classes.

[code]public class Superclass {

public void printMethod() {
System.out.println("Printed in Superclass.");
}
}
[/code]


[code]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(); }}[/code]


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, [b]for the code above, am I correct if I say the execution path in the main() method goes like this?
[/b]

[code]
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
[/code]

Share this post


Link to post
Share on other sites
No, this is more correct:

[code]
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
[/code]


In which case the output should be:


[code]Printed in SuperClass.
Printed in Subclass.
[/code]


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

Share this post


Link to post
Share on other sites
[quote]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 this post


Link to post
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.

[quote name='apatriarca' timestamp='1302952319' post='4799101']
[quote]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.


[/quote]


Thanks!

Share this post


Link to post
Share on other sites
[quote]
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 this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1303118636' post='4799815']
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.
[/quote]


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:

[code]
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;
}
}
[/code]

The "[b]super.paintComponent(g)[/b]" 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 this post


Link to post
Share on other sites
[quote name='tom_mai78101' timestamp='1303130266' post='4799851']
The "[b]super.paintComponent(g)[/b]" 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.
[/quote]

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 this post


Link to post
Share on other sites
[quote name='BitMaster' timestamp='1303130671' post='4799855']
[quote name='tom_mai78101' timestamp='1303130266' post='4799851']
The "[b]super.paintComponent(g)[/b]" 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.
[/quote]

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 this post


Link to post
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 this post


Link to post
Share on other sites
[quote name='tom_mai78101' timestamp='1303201540' post='4800251']
Thanks for the advice, even though the book itself speaks of Swing in Chapter 3. Thanks to all.
[/quote]

The problem here is that if I read something like "I could think that paintComponent() is a class placed within a class", I cannot help but believe you either need to take a much longer look at the first two chapters or should pick a different book.

Share this post


Link to post
Share on other sites
[quote]
I could think that paintComponent() is a class placed within a class...[/quote]
Nope, it is just a normal function.

[quote]
...and that "Graphics g" is passed through with no data, and gets initialized within the super.paintComponent()...[/quote]
Nope, you could call methods on "g" immediately if you want. If you wanted you could handle all the rendering yourself and not call super.paintComponent() at all.

It is impossible to initialise a reference in a different function because Java object references are passed by value. An analogy with C++ is that it is like passing a pointer, you can modify what the pointer points to, but not the pointer in the calling code itself:
[code]
#include <iostream>

int x = 13;
int y = 42;

void function(int *pointer)
{
*pointer = 1;
pointer = &y;
*pointer = 2;
}

int main()
{
std::cout << "&X: " << &x << " &Y: " << &y << '\n';
int *pointer = &x;

std::cout << "X: " << x << " Y: " << y << '\n';
std::cout << "P: " << pointer << " *P: " << *pointer << '\n';

function(pointer);

std::cout << "X: " << x << " Y: " << y << '\n';
std::cout << "P: " << pointer << " *P: " << *pointer << '\n';
}
[/code]
If you run this code you'll see that "pointer" is still pointing at x in main(). Likewise, in Java objects behave similarly:
[code]
public class Example
{
public static void initialise(String string)
{
string = "Hello, World";
}

public static void main(String [] args)
{
String s = null;
initialise(s);
System.out.println(s);
}

}
[/code]
If you run this code, you'll see that it prints null.

[quote]
...super.paintComponent() constructor.[/quote]
This is a normal function call, not a constructor. It is only the call [b]super(/*...*/)[/b] at the beginning of a constructor that invokes the super constructor. Anywhere else you can only call a superclass function.

[quote]
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. [/quote]
As BitMaster points out, the source might not help you here. Given your inexperience with Java, I don't know you are in a position where having the source will help, it might even hinder you by pulling you into the deep end.

I would suggest you step back from Swing for a bit until you have a better grasp of the core language.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this