kesea
asked on
JApplet type checking?
I am a little new to Java and I am trying to use inheritance, I am making a Generic Frame that can be called from a few applets that I have. I am passing the JApplet into the JFrame, and I would just like to check what type it is?
So I would like to check weather the mApplet_t object is either of class type Applet1, Applet2 or Applet3
switch mApplet_t.getClass()
{
case (ClassTYPE)Applet1:
(Applet1)mApplet.funcForAp plet1();
break;
case (ClassTYPE)Applet2:
(Applet2)mApplet.funcForAp plet2();
break;
case (ClassTYPE)Applet3:
(Applet3)mApplet.funcForAp plet3();
break;
}
This is what I want to do, but I can't seem to find any documentation on how to do it. Thanks experts.
So I would like to check weather the mApplet_t object is either of class type Applet1, Applet2 or Applet3
switch mApplet_t.getClass()
{
case (ClassTYPE)Applet1:
(Applet1)mApplet.funcForAp
break;
case (ClassTYPE)Applet2:
(Applet2)mApplet.funcForAp
break;
case (ClassTYPE)Applet3:
(Applet3)mApplet.funcForAp
break;
}
This is what I want to do, but I can't seem to find any documentation on how to do it. Thanks experts.
Use instanceof
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
But you cannot use switch for this. You should use if-else structure as CEHJ showed above.
!!???
kesea ,
This should be a split or all points should go to CEHJ. Want the question to be reopened so you can redestribute the points?
Venabili
This should be a split or all points should go to CEHJ. Want the question to be reopened so you can redestribute the points?
Venabili
As the calls are being made from seperate appets, you can avoid having an if statement at all by having the applets call the seperate methods directly.
> (Applet1)mApplet.funcForAp plet1();
> (Applet2)mApplet.funcForAp plet2();
> (Applet3)mApplet.funcForAp plet3();
In fact why don't you make the method name the same in all three applets.
Then you wouldn't need an if and could just use:
mApplet.funcForApplet();
> (Applet2)mApplet.funcForAp
> (Applet3)mApplet.funcForAp
In fact why don't you make the method name the same in all three applets.
Then you wouldn't need an if and could just use:
mApplet.funcForApplet();
objects, read the question :)
The object name is mApplet_t
mApplet seems to be just a class with all the methods in it :)
The object name is mApplet_t
mApplet seems to be just a class with all the methods in it :)
> mApplet seems to be just a class with all the methods in it :)
then why is casting needed?
And the fact that they are trying to use inheritance also seems to suggest that a design change would remove the need for any conditional code.
then why is casting needed?
And the fact that they are trying to use inheritance also seems to suggest that a design change would remove the need for any conditional code.
you can create an interface defing the method:
public interface MyInterface
{
public void funcForApplet();
}
public class Applet1 extends JApplet implements MyInterface
{
public void funcForApplet()
{
// do whatever
}
...
}
public class Applet2 extends JApplet implements MyInterface
{
public void funcForApplet()
{
// do whatever
}
...
}
public class Applet3 extends JApplet implements MyInterface
{
public void funcForApplet()
{
// do whatever
}
...
}
and then you no longer need a switch or if and can just use:
((MyInterface)mApplet_t).f uncForAppl et();
public interface MyInterface
{
public void funcForApplet();
}
public class Applet1 extends JApplet implements MyInterface
{
public void funcForApplet()
{
// do whatever
}
...
}
public class Applet2 extends JApplet implements MyInterface
{
public void funcForApplet()
{
// do whatever
}
...
}
public class Applet3 extends JApplet implements MyInterface
{
public void funcForApplet()
{
// do whatever
}
...
}
and then you no longer need a switch or if and can just use:
((MyInterface)mApplet_t).f
Can you explain why my solution would not help, especially considering you are trying to use inheritance?
objects' answer is best (can't think why i didn't think of it myself ;-))