3 Replies - 817 Views - Last Post: 04 January 2012 - 09:17 AM Rate Topic: -----

#1 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

The 'super' Keyword

Posted 01 January 2012 - 10:14 PM

Hello DIC, I've recently been making a game and I ran into a situation where I needed to use 'super'. Now, I sort of understand it, but sort of understanding your language doesn't make you a good programmer. So, what does super really do?

I've searched on google and all the websites I've looked at give a vague description.

Thank you!
~Crockeo
Is This A Good Question/Topic? 0
  • +

Replies To: The 'super' Keyword

#2 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: The 'super' Keyword

Posted 01 January 2012 - 10:39 PM

*
POPULAR

Super lets you access the super class (the class which you 'extend'). Example:

public class myClass extends JFRame 
{
   public myClass()
   {
      super.setTitle("Will set the JFrames title to this text");
      // Basically, since the 'superclass' (which is JFrame) has a method called setTitle, you can call it using the super.setTitle. This is a bad example though since myClass is already a JFrame, and you could use this.setTitle() also. You would most likely use it when you override a method from the super class.
   }

   // This class has overriden the JFrames method
   public void setTitle(String s)
   {
      super.setTitle(s);
      // Ok, this time you are still doing the same thing. the JFrames method setTile will run normally as your calling the method from the super class, not myClass. If you were to use this.setTitle, you would call this method in myClass not in JFrame.
      // If you didnt add super.setTitle, the title of the frame would not be changed, it would only print out what is below since you have overridden the method. 
      System.out.println("You have changed the title);

   }

}


Sorry if that wasn't clear, but I was never good at exlaining. In short though:

Super - Access the class which you have extended and any methods/fields it has (except for private).
this - Access the current class and it's fields/methods

You can also use super for the superclasses constructors. Example: JFrame has multiple constructors, and you might not want to use the default one:

public class myClass extends JFRame 
{
   public myClass()
   {
      super("Single string constructor for JFrame");
      // this will invoke the constructor JFrame(String s) in JFrame instead of JFrame() which it wouldbe by default;

   }

}

This post has been edited by Mylo: 01 January 2012 - 10:44 PM

Was This Post Helpful? 7
  • +
  • -

#3 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

Re: The 'super' Keyword

Posted 02 January 2012 - 02:37 PM

Thank you, I think I understand it.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,167
  • Joined: 27-December 08

Re: The 'super' Keyword

Posted 04 January 2012 - 09:17 AM

Adding another example, take a look at the paint() method. When you override paint(), you generally invoke super.paint() first thing. This allows you to more easily utilize key superclass functionality without rewriting the code. It goes towards modularity and encapsulation.
class MyPanel extends JPanel{

    public void paint(Graphics g){
        super.paint(g);
        //your custom painting
    }
}



The key OOP term here is polymorphism, specifically method overriding. Check out the Oracle tutorial for more information.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1