Results 1 to 18 of 18
  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Can someone explain...

    What is the keyword super? I don't quite understand what it does. I'm using it in a tutorial but I don't feel comfortable continuing without knowing what it is. Can someone help me out?

    Also, what is the term casting?

  2. #2
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Can someone explain...

    I'm new to this forum, no one has instructed me about how to post headers on this forum.
    With all due respect, if you would have liked to address me about it, it would've been more efficient to Private Message me, than to spam my post and not give me an answer to the original post.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Can someone explain...

    Be specific from esr's smart question faq.

    (which - I have just noticed is also linked to from the faq on this site: Forum Rules)

  4. #4
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Can someone explain...

    I've read that super has something to do with superclasses, but, that is where it gets confusing. I'm guessing it overrides the use of using either, a) a object for the class it corresponds with, or a b) private class/protected class it corresponds with.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can someone explain...

    First off, a super class is the immediate parent class of the child class (in an inheritance hierarchy), and the term "super" can be used in a child's constructor to call the super class's constructor of your choice. So if the super class has one constructor that takes no parameters and another that takes an int, you can tell the child's constructor to call the one that takes the int if you call super(myInt). Otherwise the parameterless constructor will be called by default. This has to be on the first line of the child's constructor to work.

    The super key word can also be used to call any of the super class's methods.

    As for casting -- you will find much on this (and on the super key word) in tutorials via Google.

  6. #6
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Can someone explain...

    Java Code:
    import javax.swing.JFrame;
    
    public class Main{
    	public static void main( String [] args) {
    	JFrame frame = new JFrame("Star Blasters II");
    	frame.setSize(800, 600);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setResizable(false);
    	frame.add(new GameFrame());
    	frame.setVisible(true);
    	}
    }
    Java Code:
    import javax.swing.JFrame;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    
    public class GameFrame extends JPanel {
    	public GameFrame() {
    		setFocusable(true);
    	}
    	
    	public void paint(Graphics g) {
    		super.paint(g);
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.drawString("test", 100 , 100);
    		
    	}
    }
    When using these two classes, and I try to compile the GameFrame class, I get this error.


  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can someone explain...

    You've got an import problem. Can you figure out what is missing based on this hint? :)

  8. #8
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Can someone explain...

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    
    public class GameFrame extends JPanel {
    	public GameFrame() {
    		setFocusable(true);
    	}
    	
    	public void paint(Graphics g) {
    		super.paint(g);
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.drawString("test", 100 , 100);
    		
    	}
    }
    I imported JPanel, and it compiles that correctly, but in my mainclass, I get this error when I compile, this tut is turning out worse and worse every time!


  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can someone explain...

    Quote Originally Posted by Army View Post
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    
    public class GameFrame extends JPanel {
    	public GameFrame() {
    		setFocusable(true);
    	}
    	
    	public void paint(Graphics g) {
    		super.paint(g);
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.drawString("test", 100 , 100);
    		
    	}
    }
    I imported JPanel, and it compiles that correctly, but in my mainclass, I get this error when I compile
    Did you try to compile Main before successfully compiling GameFrame? Are they in the same directory/package?

    ...this tut is turning out worse and worse every time!
    I don't think that the tutorial is at fault, but rather that you're new to the language, and it will take many little steps to get things started, but once over the hump things will go much better. Expect to be thrown more speed bumps on this journey, but don't give up yet.

  10. #10
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Can someone explain...

    I'm not sure I know how to make a package on Notepad++. Am I ready to use an IDE, Fubarable-Sensai?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can someone explain...

    No stay with Notepad++ for now Army-san.

  12. #12
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Can someone explain...

    How do you make a package on Notepad++ then, sensai?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  14. #14
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Can someone explain...

    Wow, I am facepalming right now. I looked at what is a interface, and the class I'm taking now basically copied and pasted all that stuff to their online course.

    In other news, I'm guessing you can... "declare" (excuse my terminology) a package by saying - package Something; at the top?

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can someone explain...

    Quote Originally Posted by Army View Post
    Wow, I am facepalming right now. I looked at what is a interface, and the class I'm taking now basically copied and pasted all that stuff to their online course.

    In other news, I'm guessing you can... "declare" (excuse my terminology) a package by saying - package Something; at the top?
    Yes, but you'd better be very sure that your code is in a directory structure that matches the package structure, and that you compile and run your code taking packages into account.

  16. #16
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Can someone explain...

    Quote Originally Posted by Fubarable View Post
    Yes, but you'd better be very sure that your code is in a directory structure that matches the package structure, and that you compile and run your code taking packages into account.
    Do you have a MSN, lol. But, anyway, is there a way for me to compile and run a package all together, like '>javac Something.pack' lol.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  18. #18
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Can someone explain...

    Thanks for being so patient, you should see my tabs.

    Anyway, even if import everything right on this step, the tutorial does not give me enough information later on, for he is using an IDE and he basically is clicking to make methods and imports. I cannot follow everything he is doing :/.

Similar Threads

  1. can someone explain what this exactly does?
    By liluma in forum New To Java
    Replies: 4
    Last Post: 08-21-2011, 08:58 PM
  2. Replies: 2
    Last Post: 12-13-2010, 02:34 PM
  3. Replies: 3
    Last Post: 12-13-2010, 08:22 AM
  4. Can somebody explain me this plz
    By ccie007 in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 08:47 PM
  5. Can someone explain why...
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2009, 07:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •