super() question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: super() question

  1. #1
    Join Date
    Jan 2005
    Posts
    45

    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();

    }

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    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

  3. #3
    Join Date
    Jan 2005
    Posts
    45
    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.

  4. #4
    Join Date
    Sep 2004
    Posts
    223
    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

  5. #5
    Join Date
    Feb 2004
    Posts
    541
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center