469,352 Members | 1,697 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Creating an Interface that Extends Another Class

I'm creating a sequence of JPanels that will each, in turn, be added to
(then removed from) a window (kind of like a wizard). I want these panels
to not only extend the JPanel class, but to implement an interface. I have
the interface defined in a separate class, like this:
public interface IPanel {
boolean onNext();
String nextPanel();
boolean hasCancel();
}

This leads to several problems. If I create one of my panels with:

JPanel myPanel = new JPanel();

and in the class definition line, I have:

class MySpecialPanel extends JPanel implements IPanel {
yada, yada, yada
}

If another class references one of these methods specified by IPanel, I get
error messages stating that method is not in JPanel.

If I define a new class with:

IPanel myPanel = new IPanel();

then try to add the new panel to an existing JPanel, I get an error message
that I can't do that with an IPanel and need to use a JPanel.

So even though the class states it extends JPanel and implements IPanel,
when I compile, I can't call an IPanel method if I define it as a JPanel
and I can't use the class as a JPanel if I define it as an IPanel.

It seems to me the perfect fix would be to create the interface and say it
extends JPanel, like this:

public interface IPanel extends JPanel {
blah, blah, blah
}

but I get errors when I try that and try to compile it. Is there any way to
specify IPanel as an interface that extends JPanel, so I can add IPanels
when I'd normally add JPanels?

Or is there a better way to handle this?

Thanks!

Hal
Jul 17 '05 #1
3 23115

"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:eg4jb.560923$Oz4.520446@rwcrnsc54...
I'm creating a sequence of JPanels that will each, in turn, be added to
(then removed from) a window (kind of like a wizard). I want these panels
to not only extend the JPanel class, but to implement an interface. I have the interface defined in a separate class, like this:
public interface IPanel {
boolean onNext();
String nextPanel();
boolean hasCancel();
}

This leads to several problems. If I create one of my panels with:

JPanel myPanel = new JPanel();

and in the class definition line, I have:

class MySpecialPanel extends JPanel implements IPanel {
yada, yada, yada
}

If another class references one of these methods specified by IPanel, I get error messages stating that method is not in JPanel.

If I define a new class with:

IPanel myPanel = new IPanel();

then try to add the new panel to an existing JPanel, I get an error message that I can't do that with an IPanel and need to use a JPanel.

So even though the class states it extends JPanel and implements IPanel,
when I compile, I can't call an IPanel method if I define it as a JPanel
and I can't use the class as a JPanel if I define it as an IPanel.

It seems to me the perfect fix would be to create the interface and say it
extends JPanel, like this:

public interface IPanel extends JPanel {
blah, blah, blah
}

but I get errors when I try that and try to compile it. Is there any way to specify IPanel as an interface that extends JPanel, so I can add IPanels
when I'd normally add JPanels?

Or is there a better way to handle this?

Thanks!

Hal


Yes, simply use MySpecialPanel where you need both JPanel and IPanel
functionality.

Silvio Bierman
Jul 17 '05 #2
SPG
Hi,

Another solution is to use IPanel as an abstract class.

public abstract class IPanel extends JPanel{
//Don;t forget your constructors!
public IPanel(........){
super(.......);
}

public abstract boolean onNext();
public abstract String nextPanel();
public abstract boolean hasCancel();
}

Then you can simply create your panel as you like and have access to both
interfaces

HTH,

Steve

"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:eg4jb.560923$Oz4.520446@rwcrnsc54...
I'm creating a sequence of JPanels that will each, in turn, be added to
(then removed from) a window (kind of like a wizard). I want these panels
to not only extend the JPanel class, but to implement an interface. I have the interface defined in a separate class, like this:
public interface IPanel {
boolean onNext();
String nextPanel();
boolean hasCancel();
}

This leads to several problems. If I create one of my panels with:

JPanel myPanel = new JPanel();

and in the class definition line, I have:

class MySpecialPanel extends JPanel implements IPanel {
yada, yada, yada
}

If another class references one of these methods specified by IPanel, I get error messages stating that method is not in JPanel.

If I define a new class with:

IPanel myPanel = new IPanel();

then try to add the new panel to an existing JPanel, I get an error message that I can't do that with an IPanel and need to use a JPanel.

So even though the class states it extends JPanel and implements IPanel,
when I compile, I can't call an IPanel method if I define it as a JPanel
and I can't use the class as a JPanel if I define it as an IPanel.

It seems to me the perfect fix would be to create the interface and say it
extends JPanel, like this:

public interface IPanel extends JPanel {
blah, blah, blah
}

but I get errors when I try that and try to compile it. Is there any way to specify IPanel as an interface that extends JPanel, so I can add IPanels
when I'd normally add JPanels?

Or is there a better way to handle this?

Thanks!

Hal

Jul 17 '05 #3
Hal Vaughan <ha*@thresholddigital.com> wrote in message news:<eg4jb.560923$Oz4.520446@rwcrnsc54>...
I'm creating a sequence of JPanels that will each, in turn, be added to
(then removed from) a window (kind of like a wizard). I want these panels
to not only extend the JPanel class, but to implement an interface. I have
the interface defined in a separate class, like this:
public interface IPanel {
boolean onNext();
String nextPanel();
boolean hasCancel();
}

This leads to several problems. If I create one of my panels with:

JPanel myPanel = new JPanel();

and in the class definition line, I have:

class MySpecialPanel extends JPanel implements IPanel {
yada, yada, yada
}

If another class references one of these methods specified by IPanel, I get
error messages stating that method is not in JPanel.

If I define a new class with:

IPanel myPanel = new IPanel();

then try to add the new panel to an existing JPanel, I get an error message
that I can't do that with an IPanel and need to use a JPanel.

So even though the class states it extends JPanel and implements IPanel,
when I compile, I can't call an IPanel method if I define it as a JPanel
and I can't use the class as a JPanel if I define it as an IPanel.

It seems to me the perfect fix would be to create the interface and say it
extends JPanel, like this:

public interface IPanel extends JPanel {
blah, blah, blah
}

but I get errors when I try that and try to compile it. Is there any way to
specify IPanel as an interface that extends JPanel, so I can add IPanels
when I'd normally add JPanels?

Or is there a better way to handle this?

Thanks!

Hal

If you want to work with your class as MySpecialPanel, you have to instantiate:
MySpecialPanel aPanel = new MySpecialPanel();

If you want to handle this class as IPanel, you have to do:
IPanel aPanel = new MySpecialPanel();

But this line of code will never work:
IPanel myPanel = new IPanel();
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Rennie deGraaf | last post: by
6 posts views Thread by Donal McWeeney | last post: by
15 posts views Thread by Carlos Lozano | last post: by
1 post views Thread by Marcel Hug | last post: by
4 posts views Thread by Zerofury | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.