TBNRsiyi
TBNRsiyi

Reputation: 23

Extending from JFrame

Is there a difference from extending to JFrame and javax.swing.JFrame?

Example:

public class Authenticator extends JFrame {

and...

public class Authenticator extends javax.swing.JFrame {

Upvotes: 1

Views: 544

Answers (4)

Azmat Karim Khan
Azmat Karim Khan

Reputation: 477

If you import javax.swing.JFrame; then you use public class Authenticator extends JFrame { if not then use public class Authenticator extends jvax.swing.JFrame {
But the second method is mostly used when you have classes with same name in different pakage, to differentiate the classes.
for example
[-]mypackage
 |----[-]pakage1
              |---TestClass.java
 |----[-]pakage2
        |---TestClass.java

Here is the situation we have a package named mypackage and two sub packages pakage1 and pakage2

now if we just import it will give this

import mypackage.pakage1.TestClass;
import mypackage.pakage2.TestClass;
class Testw
{
public static void main(String []args)
{
System.out.println("Swah!");
}
}

it will give following error

C:\Program Files\Java\jdk1.6.0_38\bin>javac Testw.java
Testw.java:2: mypackage.pakage1.TestClass is already defined in a single-type import
import mypackage.pakage2.TestClass;
^
1 error


so what you do?
In this case you use the second method which is also called fully quailfied name now you import one pakage and use fully qualified name for other.

import mypackage.pakage1.TestClass;
class Testw
{
public static void main(String []args)
{
TestClass  testclass1 = new TestClass();
mypackage.pakage2.TestClass  testclass2 = new  mypackage.pakage2.TestClass();
System.out.println("Swah!");
}
}

So the summery of whole thing is that fully qualified name is used when their is name clashing, we can also use this method when their is no name clashing ,their will be no -ve effect on program

Upvotes: 1

Doodad
Doodad

Reputation: 1518

It makes no difference unless you have another class called JFrame and are importing it instead of javax.swing.JFrame.

That said, as Andrew Thompson said, you shouldn't extend JFrame, you should use an instance.

Upvotes: 6

frauneworld
frauneworld

Reputation: 72

you have to import JFrame anyway, and it will be from javax.swing package most likely, you only need to write the whole package path if there are multiple JFrame classes included, so the compiler will know which one you want to use, so if there's only one JFrame class, then the import is enough, you don't have to specify it again

Upvotes: 0

mael
mael

Reputation: 2254

It depends on your import. If you import javax.swing.JFrame, no.

Upvotes: 0

Related Questions