[foo.bar.Frame] can not inherit from [java.awt.Frame]

Discussion in 'Java' started by Maciej Labanowicz, Apr 11, 2012.

  1. Hi,

    I'm trying to compile simple class, named Frame in package foo.bar:

    --[beg]--[Frame.java]-----------------------------------
    package foo.bar;
    import java.awt.Frame;
    public class Frame extends java.awt.Frame {
    public static final long serialVersionUID = 1L;
    public static void main(String [] args) {
    System.out.println("HELLO: args = " + args);
    }
    }
    --[eof]--[Frame.java]-----------------------------------

    /> javac Frame.java
    Frame.java:2: error: foo.bar.Frame is already defined in this
    compilation unit
    import java.awt.Frame;
    ^
    1 error

    I have also tried with:
    - public class Frame extends java.awt.Frame {
    + public class foo.bar.Frame extends java.awt.Frame {

    But the error is:
    Frame.java:3: error: '{' expected
    public class foo.bar.Frame extends java.awt.Frame {
    ^
    1 error

    Is there any possibility to create such class in java ?

    Regards

    --
    Maciek
     
    Maciej Labanowicz, Apr 11, 2012
    #1
    1. Advertising

  2. Maciej Labanowicz

    Paul Cager Guest

    Re: can not inherit from [java.awt.Frame]

    On Apr 11, 12:56 pm, Maciej Labanowicz <> wrote:
    > /> javac Frame.java
    > Frame.java:2: error: foo.bar.Frame is already defined in this
    > compilation unit
    > import  java.awt.Frame;
    > ^
    > 1 error


    That should work. Are you sure you have shown us the whole file?

    One problem that would lead to these symptoms is if you had
    accidentally duplicated the class declaration in the file, such as
    (untested):

    package foo;

    public class Frame {
    // Lots of stuff
    }

    public class Frame {
    // Lots of stuff
    }

    If that's not the case, which compiler are you using?
     
    Paul Cager, Apr 11, 2012
    #2
    1. Advertising

  3. Maciej Labanowicz <> wrote:
    > I'm trying to compile simple class, named Frame in package foo.bar:
    > --[beg]--[Frame.java]-----------------------------------
    > package foo.bar;
    > import java.awt.Frame;


    This import statement tells the Java compiler, that when it
    encounters a bare Frame literal, that it should consider that
    from java.awt.Frame. This obviously cannot work, if you have
    a different class Frame (foo.bar.Frame) already.

    Solution 1 would be to call your class differently, like MyFrame

    Solution 2 would be to remove the "import" statement for java.awt.Frame.
    After doing so, ...
    > - public class Frame extends java.awt.Frame {

    this one ought to work.
     
    Andreas Leitgeb, Apr 11, 2012
    #3
  4. Maciej Labanowicz

    Lew Guest

    Re: can not inherit from [java.awt.Frame]

    On 04/11/2012 05:51 AM, Paul Cager wrote:
    > On Apr 11, 12:56 pm, Maciej Labanowicz<> wrote:
    >> /> javac Frame.java
    >> Frame.java:2: error: foo.bar.Frame is already defined in this
    >> compilation unit
    >> import java.awt.Frame;
    >> ^
    >> 1 error

    >
    > That should work. Are you sure you have shown us the whole file?


    No, it shouldn't work.

    > One problem that would lead to these symptoms is if you had
    > accidentally duplicated the class declaration in the file, such as
    > (untested):
    >
    > package foo;
    >
    > public class Frame {
    > // Lots of stuff
    > }
    >
    > public class Frame {
    > // Lots of stuff
    > }
    >
    > If that's not the case, which compiler are you using?


    He's using a standards-conforming compiler, which is all that matters in this
    case.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
     
    Lew, Apr 11, 2012
    #4
  5. Maciej Labanowicz

    markspace Guest

    On 4/11/2012 4:56 AM, Maciej Labanowicz wrote:

    > Frame.java:2: error: foo.bar.Frame is already defined in this
    > compilation unit



    I *think* what the compiler is saying using Frame as an unqualified name
    collides with the existing foo.bar.Frame usage.

    The solution is just to not use the "import" statement.

    package quicktest;

    class Frame extends java.awt.Frame {
    public static final long serialVersionUID = 1L;
    public static void main(String [] args) {
    System.out.println("HELLO: args = " + args);
    }
    }

    If your compiler can find "import java.awt.Frame", then it can find
    java.awt.Frame without the import. You don't need import, it just
    allows you to shorten the FQN to just the class name.
     
    markspace, Apr 11, 2012
    #5
  6. Maciej Labanowicz

    Paul Cager Guest

    Re: can not inherit from [java.awt.Frame]

    On Apr 11, 4:13 pm, Lew <> wrote:
    > On 04/11/2012 05:51 AM, Paul Cager wrote:


    > > That should work. Are you sure you have shown us the whole file?

    >
    > No, it shouldn't work.


    Ah yes, you're right. I hadn't seen that import.
     
    Paul Cager, Apr 11, 2012
    #6
  7. Maciej Labanowicz

    markspace Guest

    On 4/11/2012 9:34 AM, Patricia Shanahan wrote:

    > I think a better solution would be to rename foo.bar.Frame. If it did
    > not have some functional difference from java.awt.Frame, it would not
    > exist. Presumably, it is Frame specialized in some way, and its name
    > should indicate that specialization.



    Well, its full name "foo.bar.Frame" should indicate its specialization.
    A better name like com.application.main.Frame would be a more likely
    choice if this weren't a quick example on the internet. In general I
    don't have a problem with using common words as class names in different
    packages. It actually adds to clarity in many cases.

    OTOH, if the actual use of foo.bar.Frame and java.awt.Frame gets to be a
    pain to deal with, then it certainly should be renamed. Confusion or
    even just convenience is a sufficient reason for refactoring the name of
    a class.
     
    markspace, Apr 11, 2012
    #7
  8. Maciej Labanowicz

    Daniel Pitts Guest

    On 4/11/12 4:56 AM, Maciej Labanowicz wrote:
    > Hi,
    >
    > I'm trying to compile simple class, named Frame in package foo.bar:
    >
    > --[beg]--[Frame.java]-----------------------------------
    > package foo.bar;
    > import java.awt.Frame;
    > public class Frame extends java.awt.Frame {
    > public static final long serialVersionUID = 1L;
    > public static void main(String [] args) {
    > System.out.println("HELLO: args = " + args);
    > }
    > }
    > --[eof]--[Frame.java]-----------------------------------

    As others have pointed out, the problem is that you import Frame. you
    don't need to import something if you use the fully qualified name.

    A side note, extending Frame is probably not what you really want to do.
    It is unfortunate that so many examples and tutorials have you do this,
    but in reality, you just need to compose a Frame with the various
    Components you need. For the most part, the only time you need to
    extend *any* Component or JComponent is when you are creating a custom
    view of a specific type of data. And then you should only extend
    JComponent or Canvas as I understand it.


    hth,
    Daniel.
     
    Daniel Pitts, Apr 11, 2012
    #8
  9. Maciej Labanowicz

    Lew Guest

    Daniel Pitts wrote:
    > Maciej Labanowicz wrote:
    >> I'm trying to compile simple class, named Frame in package foo.bar:
    >>
    >> --[beg]--[Frame.java]-----------------------------------
    >> package foo.bar;
    >> import java.awt.Frame;
    >> public class Frame extends java.awt.Frame {
    >> public static final long serialVersionUID = 1L;
    >> public static void main(String [] args) {
    >> System.out.println("HELLO: args = " + args);
    >> }
    >> }
    >> --[eof]--[Frame.java]-----------------------------------


    > As others have pointed out, the problem is that you import Frame. you
    > don't need to import something if you use the fully qualified name.
    >
    > A side note, extending Frame is probably not what you really want to do.
    > It is unfortunate that so many examples and tutorials have you do this,
    > but in reality, you just need to compose a Frame with the various
    > Components you need. For the most part, the only time you need to
    > extend *any* Component or JComponent is when you are creating a custom
    > view of a specific type of data. And then you should only extend
    > JComponent or Canvas as I understand it.


    This raises another question, OP: Why do you use 'java.awt.Frame' instead of 'javax.swing.JFrame'?

    --
    Lew
     
    Lew, Apr 11, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    404
    John Roth
    Jul 29, 2005
  2. Jonathan Bartlett
    Replies:
    7
    Views:
    467
    Peter Nilsson
    Jul 8, 2005
  3. Wejn
    Replies:
    2
    Views:
    133
    Michal
    Nov 29, 2003
  4. Max Williams
    Replies:
    10
    Views:
    218
    Max Williams
    Dec 15, 2007
  5. Gunnar Hjalmarsson
    Replies:
    12
    Views:
    355
    Darren Dunham
    Feb 24, 2005
Loading...

Share This Page