Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Is it possible for me to simplify this code to not use "super" here - I did not understand this

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is it possible for me to simplify this code to not use "super" here - I did not understand this

    public class Reset extends JButton {
    Reset() {
    super("Reset");
    super.setText("Reset");
    int h = 20, w = 20;
    int x = 0 * 20; int y = 7 * 30;
    setBounds(x, y, h, w);
    addActionListener(new ResetActionListener()); setVisible(true);
    }
    private class ResetActionListener implements ActionListener {
    public void actionPerformed(ActionEvent ae) {
    ConnectClient.reset();
    }
    }
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Is it possible for me to simplify this code to not use "super" here - I did not understand this

    Do you have a question?

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is it possible for me to simplify this code to not use "super" here - I did not understand this

    super("text") can only be called once and it has to be the first line of the method/constructor.

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is it possible for me to simplify this code to not use "super" here - I did not understand this

    is there an alternative to using super in the code snippet above

  5. #5
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Is it possible for me to simplify this code to not use "super" here - I did not understand this

    Yes. Do not extend JButton, use one instead. There is absolutely no point extending JButton if that's all you do.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Is it possible for me to simplify this code to not use "super" here - I did not understand this

    In addition to the suggestion on extending JButton when it is not necessary, the two lines of code:
    super("Reset");
    super.setText("Reset");
    The call to super with a String creates a JButton with the text set to "Reset"
    The call to super.setText changes the text from "Reset" to "Reset"
    You can use the super.setText("Reset") and delete the call to the constructor, or delete this line and leave the constructor call with the desired text.

Similar Threads

  1. Conceptual Questions Related To "super" keyword and "constructor".
    By wikki2013 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 26th, 2013, 02:14 PM
  2. Trying to understand the "protected" keyword.
    By simpson_121919 in forum Object Oriented Programming
    Replies: 3
    Last Post: April 30th, 2013, 08:57 PM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM