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

M

Maciej Labanowicz

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
 
P

Paul Cager

/> 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?
 
A

Andreas Leitgeb

Maciej Labanowicz said:
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.
 
L

Lew

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.
 
M

markspace

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.
 
M

markspace

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.
 
D

Daniel Pitts

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.
 
L

Lew

Daniel said:
Maciej said:
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'?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top