Results 1 to 4 of 4
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Quick question about import and inheretence

    Hi,
    I have two classes, the first is with my main() function and it has this:
    import javax.swing.JFrame;
    public class GUI {
    I have bolded the part that I want to talk about, and my second class has this:

    import java.awt.FlowLayout; // gets the flowlayout
    import javax.swing.JFrame; // gets the whole pane with the minimize,maximize, x button etc
    import javax.swing.JLabel;

    public class GUI_Sub extends JFrame{
    My question:
    How come in the main class I have to import like this "javax.swing.JFrame" but in the second class (which does not have a main() method and gets called by the first class) all I say is " extends JFrame" and thats enough?
    Shouldnt it be "extends javax.swing.JFrame"?
    How does Java know which "JFrame" I am talking about?


    Thanks!
    Last edited by N00Bie; 02-13-2011 at 01:57 PM.

  2. #2
    TheEnemy is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    I think you've missed that you also has an import for javax.swing.JFrame in your second class. So it knows you mean javax.swing.JFrame for the extend. If you were to remove that import, you'd have to specify the complete path to JFrame in the extends line.

    Java Code:
    import java.awt.FlowLayout; // gets the flowlayout
    [B]// (removed) import javax.swing.JFrame;[/B] // gets the whole pane with the minimize,maximize, x button etc
    import javax.swing.JLabel;
    
    public class GUI_Sub extends [B]javax.swing.JFrame[/B]{

  3. #3
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Ah! That makes sense! Thanks!
    (Also like your sig... so true... so damn true!)

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    To make it clearer, an import does not "import" anything. It is simply a short-hand for the programmer (and a reference for the compiler) so that the programmer, in the code, does not have to type the entire package path for the classes being referenced.

Similar Threads

  1. Quick question
    By Thumper in forum New To Java
    Replies: 10
    Last Post: 11-07-2010, 10:06 PM
  2. Really quick question
    By shadycharacter in forum New To Java
    Replies: 2
    Last Post: 04-22-2010, 10:06 PM
  3. One last quick question
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 08:53 AM
  4. Quick Question
    By Graeme in forum New To Java
    Replies: 4
    Last Post: 01-08-2009, 08:01 PM
  5. Quick Question
    By Spenc in forum New To Java
    Replies: 3
    Last Post: 09-22-2008, 02:26 PM

Posting Permissions

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