Solved

JApplet type checking?

Posted on 2004-08-20
14
226 Views
Last Modified: 2010-03-31
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.funcForApplet1();
        break;
        case (ClassTYPE)Applet2:
                (Applet2)mApplet.funcForApplet2();
        break;
        case (ClassTYPE)Applet3:
                (Applet3)mApplet.funcForApplet3();
        break;
     }

This is what I want to do, but I can't seem to find any documentation on how to do it.  Thanks experts.
0
Comment
Question by:kesea
  • 5
  • 4
  • 3
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11852090
Use instanceof
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 11852112
if (applet instanceof Applet1) {
    // do it
}
else if ...

etc.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 11852327
But you cannot use switch for this. You should use if-else structure as CEHJ showed above.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11852601
!!???
0
 
LVL 20

Expert Comment

by:Venabili
ID: 11852605
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
0
 
LVL 92

Expert Comment

by:objects
ID: 11856833
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.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 11856863
> (Applet1)mApplet.funcForApplet1();
> (Applet2)mApplet.funcForApplet2();
> (Applet3)mApplet.funcForApplet3();

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();

0
 
LVL 20

Expert Comment

by:Venabili
ID: 11856870
objects, read the question :)

The object name is mApplet_t

mApplet seems to be just a class with all the methods in it :)
0
 
LVL 92

Expert Comment

by:objects
ID: 11857086
> 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.
0
 
LVL 92

Expert Comment

by:objects
ID: 11857132
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).funcForApplet();
0
 
LVL 92

Expert Comment

by:objects
ID: 11861882
Can you explain why my solution would not help, especially considering you are trying to use inheritance?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11863570
objects' answer is best (can't think why i didn't think of it myself ;-))
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now