-
super() question
Can someone please explain to me the use of super(t) in the following code. Thanks alot.
import javax.swing.*;
import java.awt.*;
public class ComponentExample(String t){
super(t);
Container cp = getContentPane();
cp.add(new JButton("Click", "North");
pack();
}
-
Java works in a heirarchy manner, each class is either a parent or a child of another class, but in the end they all come back to the Object class.
So when you say super(t) you are calling the constructor of the class that your class is a child of, so in your case you are calling the Object constructor and passing it the String t, not too exciting, not even necessary...
A kram a day keeps the doctor......guessing
-
How come when the string t is passed, its gonna become the title of the window created? I dont get the relationship between how that works and the fact that it passes the String to the Object constructor. Can you please explain further? Thanks a lot.
-
The constructor of the super clas that you are passing the value must set it up as on of the parameters for your window.
Its simply one of the operations of the constructor of the super class. Keeping in mind that your class has derived "from" the Object class, so its like your classes parent, and that is the relationship.
If you have a class that "Extends" JApplet for example, then the JApplet class will be the super/parent class.
A kram a day keeps the doctor......guessing
-
As it is, the code above is invalid. You seem to have grouped together the class definition and the constructor all into one thing. Maybe it should look something like this?[code]import javax.swing.*;
import java.awt.*;
public class ComponentExample extends Component{ //class definition
public ComponentExample(String t){ //constructor
super(t);
Container cp = getContentPane();
cp.add(new JButton("Click", "North");
pack();
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|