Dynamic typecasting in java...is it possible?

Hi All
i have a requirement in which i do not know the exact subclass to what i have to typecast to during compile time. I am calling it xyzclass, which i do not know till run-time.
xyz can be A or B or C class.
xyzclass xyzobj = (xyzclass) superclassofxyz
Thanks in advance.
kk.

If your XYZClass, A, B and C classes are fixed, you could try a double
dispatch pattern (aka visitor pattern). Here's the idea:public interface Visitor {
   public void isXYZClass(XYZClass visitee);
   public void isA(A visitee);
   public void isB(B visitee);
   public void isC(C visitee);
public interface Visitee {
   public void knockknock(Visitor visitor);
public class XYZClass implements Visitee {
   public void knockknock(Visitor visitor) { visitor.isXYZClass(this);
   // more code here ...
public class A extends XYZClass {
   public void knockknock(Visitor visitor) { visitor.isA(this);
   // more code here ...
public class B extends XYZClass {
   public void knockknock(Visitor visitor) { visitor.isB(this);
   // more code here ...
public class C extends XYZClass {
   public void knockknock(Visitor visitor) { visitor.isC(this);
   // more code here ...
}... your class needs to implement the Visitor interface; it simply knocks
on the door of any XYZClass (or derived class) and the Visitee simply
calls the Visitor back again through the correct method.
kind regards,
Jos

Similar Messages

  • How can i create an dynamic report in Java

    hello everybody,
    I want to make dynamic reports in Java. Report contain data and images with good layout.. such as crystal reports presentation..
    Reports fields decided at run time or we can say that it is generated according to user requirement...
    can it is possible in Java. If it is then which tool is better for it..
    please suggest me.. this is very urgent requirement for me...

    i don't know what types of tools are avaiable in market... Tools for what? You still didn't tell us what exactly you want to do.
    i just imagin that
    "An user create a database dynamically on server
    databaseNobody "dynamically creates a database".
    and he decide reports desgin according to
    his requirement..... we provide an control for this
    type possibilities....."
    so this is question is build in my mind.. so i
    forward this question to this forums..
    I am just going to check the physibility of my mind
    imagination...Feasibility you mean. IMHO, the feasibility of creating that stuff yourself is very low. Reinventing the wheel usually doesn't make it better, and it's likely to be more expensive to pay you for creating an inferior solution (not because of lack of skills, but simply because the available products had a few years time to grow, feature-wise) than to simply buy the licenses. Look at Crystal Reports or JFreeChart or Jasper Reports. Or use Google to look up J2EE-based reporting libraries. There might be more.
    Which of these is best, I don't know, I never used any of those. Also, the definition of "best" changes with the requirements. What's better, a Ferrari or a truck?

  • How to create dynamic images in java servlets?

    I want to create dynamic images in java servlet. Can servlet create dynamic images that based on the input data files? The results can be displayed in GIF, JPG..format? how can this be done? any example in internet?
    What OS do I need to install and what other requirements needed if i want to build up a servlet server?
    Thanks a lot!

    Also worth having a look at SVG http://www.w3.org/TR/2001/REC-SVG-20010904/, you can get a viewer at http://www.adobe.com/svg/ or you can use Batik http://xml.apache.org/batik/index.html to convert SVG to other formats such as JPEG.
    HH

  • Dynamic loading in Java

    I was asked following in the interview-
    What things are dynamically loaded in Java?
    How can we change this behaviour so as to load things statically?
    Can anybody clarify this for me or give some reference URLs etc?
    Thanks in advance.

    I was asked following in the interview-
    What things are dynamically loaded in Java?
    How can we change this behaviour so as to load things
    statically?
    Can anybody clarify this for me or give some reference
    URLs etc?
    Sounds like an absurdly general question to me.
    For example shared libraries are loaded dynamically. And there is no way to do it statically unless one modifies the jvm.
    Or using Class.forName() loads a class dynamically. Statically one could create a null reference which would be a 'static' load. Of course that is a rather tentative definition because java is still going to load it when the method is reached rather than on start up.
    Or one could say that the core jvm is statically loaded, for example the system class loader, since it is implemented in C (but of course it actually exists in a shared library so it is loaded dynamically as well by the OS.) All other classes are loaded dynamically and again it requires changing the JVM to alter the behavior.

  • Web Intelligence Rich Client without java - is it possible?

    Hello!
    Web Intelligence Rich Client without java - is it possible?
    How about other client as Web Intelligence Rich Client, but without java?
    Is it possible?
    Regards,
    Denis

    It's very interesting, becouse i have installed pathes: ENTERPRISECLNT03P_4-10007619.EXE, CRYSTALREPORTS03P_4-10007442.EXE, BOBJINTGRSAP03P_4-10007514.EXE and problem was solved. Moreover I have uninstalled JRE. New problem not have come.
    Regards,
    Denis

  • Why objects are  dynamically created in java

    Why objects are dynamically created in java even if dynamically created object requires more space & more memory references as compared to static object?

    I don't even know where to start...
    KAMAL.MUKI wrote:
    Why objects are dynamically created in javaWhat is the alternative?
    even if dynamically created object requires more space & more memory referencesCan you prove that?
    as compared to static object?Can you define "static object"?
    I vote "troll".

  • Linklist and dynamic array in java?

    how to define linklist object and to build dynamic array in java?

    with so little information, all one can do is pointing you to :
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html

  • PI 7.1 Access to Dynamic Configuration through Java Class

    My Current scenario involves IDCO -> FILE communications in PI 7.1 using XSL where the file name is dynamic.
    I have attempted calling a Java Class from XSL with the use SAPXMLTOOL kit option set in the adaptor and it works but I need to be able to call the transform method
    public void transform(TransformationInput arg0, TransformationOutput arg1)
    from XSL so I can access the message and update the File Name via Dynamic Configuration.
    Whilst I can call a static class with in the object I can not call transform with the correct arguments (arg0, arg1). I need access to arg0 (payload) to set/figure out the filename. I can calculate the filename in XSL and pass it to the Java but in a static method it is useless as it need to access the arg0 which is a object and obviously non-static context.
    Is this possible ? How do I get access to Dynamic Configuration from XSL through Java in 7.1 ?
    I have successfully called a static method but that is no help.
    Thanks in advance.

    Suraj,
       Unfortunately you link isnt operational but thankyou anyway, I have managed to call as Java class as per my original post, but a static method does not allow me to get a handle to the Dynamic Configuration Variables in the message.
    It seems importing the 7.0 LIBS may be an option and Im not entirely sure I have that option.
    It certainly isnt possible to use the JDK5 XML implementation to even call a static method on an Java OBJ from XSL (obviously the USE SAPXMLTOOLKIT option is there for a reason).
    I think and again I could be wrong but direct access via XSLT..
    LINK:http://help.sap.com/saphelp_nwpi71/helpdata/EN/e2/e13fcd80fe47768df001a558ed10b6/frameset.htm
    Is what Im looking for, I shall report back success or failure but the doco seems to fit the bill. Im not sure why I wanted to instatiate a sperate Java class when it can all exist in the XSLT.

  • Dynamic typecasting for re-usability

    Hi there,
    I'm trying to dynamically cast an object to a class only known at runtime. All that is known at compile time is the name and type of a variable inside the object - and that is all that is of interest. The reason for this is to create re-usable code to allow multiple classes to be used as peers (all owned by the unknown class) together.
    I have a method (provided, so I can't change it) getOwner() that gives me the parent class, but as type Object. I need to re-cast it to the correct class to be able to access its content. I know ahead of time that the variable knownvar will exist in the parent class, and I know what type it will be.
    So instead of doing:
    if(this.getOwner().getType() == "ClassA")
      (ClassA)this.getOwner().knownvar = somevalue;
    else if(this.getOwner().getType() == "ClassB")
      (ClassB)this.getOwner().knownvar = somevalue;
    elseif ... (ad nauseum)I'd rather like to do something similar to this (this does not work):
    (this.getOwner().getType())this.getOwner().knownvar = somevalue;In fact, I'd prefer not to do the whole list of elseifs if at all possible, since it limits possible future use to pre-thought of class names - and adding some that might not exist (also bad).
    Any suggestions on how this can be done?

    Thank you for the replies! (Especially Loko for that possible workaround)
    Yes indeed, it is quite a bad design and I flinch at having to hack things so badly. It is not a clean and elegant design when done like this. Unfortunately I have no choice but to use a workaround. I'll try your suggestion and see if it can do the trick.
    duffymo - As for using if checks - that is exactly what I don't want to use exactly because it is not object oriented - but is a way to express and explain to others what kind of function I am trying to perform. I'd perfer to use something like my second code snippet where no prior knowledge of the class is needed. I need to cast, otherwise the knownvar property is not accessable. Unfortunately, like I said, I do not have control over the interface, so I need a workaround.
    There is also a small chance I might be able to convince the supplier of the owner class to implement this better by using an intermediate class that extends the one providing "Object getOwner()", returning a more specific class that expresses the presense of knownvar. I am using Java code fragments inside a third-party product, so it might not be possible to make this change within the timescales available.

  • Can I balloon engineering prints in Adobe Reader or Acrobat? Create a dynamic stamp w/ Java Script?

    I am trying to establish a very simple way to balloon prints with character numbers; below I have uploaded an example screen shot.  My end goal is for the user to select a dynamic stamp (a circle with a number in the center) and it start numbering with 1 and then increment by 1 yet the user is placing each balloon in its respective location, resetting for each PDF document, one addition would be to allow the balloon numbers to be edited like a text box after they have been created in case there is an error in balloon placement etc.  Is this possible? I have programming experience in other languages, but have only built a few dynamic stamps with time stamp information provided by java script code.
    Any assistance is greatly appreciated!

    Yes it's possible with dynamic stamps, but the code is rather messy as it has to keep a running total.
    See http://www.pdfscripting.com/public/department53.cfm for some samples (and the definitive book on creating your own dynamic stamps), and Rick Borstein's blog for some basic samples that put exhibit numbers on legal documents.

  • Dynamic reporting in Java

    Hi to all
    I want to create a dynamic reporting tool , in which user can add & delete no of columns from database table and for selected columns , user want to generate reports. Can any one give me idea about Java API or any other API that we can use in java application for dynamic reporting. Please give me reference example for suggested API if possible. I shall be very thankfull for your support & guidence.
    Preet

    socmag_Java_developer wrote:
    I want to create my own GUI reporting tool in java. Can you suggest me any Java API with some examplesIf you mean "*Standard* Java API", don't you find suggestions and examples in the first answer?

  • Dynamic FIle Name JAVA code

    Hi Guys,
    he requirement is to get File Name as XYZ.ddmmyyyy where as XYZ is constant.
    To achive this I am writing UDF in message mapping with dynamic configuaration below is the code:
    While I was trying to test end to end it is giving Runtime Exception in message mapping. please advice if this code needs correction?
    I have mapped as Source Main Node .....> UDF >  Target main node correct if it is wrong.
    DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
    DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File" , "FileName");
    DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
    Date date = new Date();
    String  filename =  "XYZ" + dateFormat.format(date);
    conf.put (key,filename);
    return null;   
    Thanks.

    Hi ,
         Please try replacing this line
    DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
    with this line
    java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("ddMMyyyy");
    Hope this solves your problem.
    regards
    Anupam

  • Dynamic Call to Java Class or Method

    Hi All,
    I have a requirement to call a Class or a Method Within the same class dynamically based on the user choice. I will give an example with how I do it in pl sql.
    begin
    if (choice = 1) then
    execute immediate 'package.procedure1(params)';
    elsif (choice = 2) then
    execute immediate 'package.procedure2(params)';
    else
    execute immediate 'package.procedure3(params)';
    end if;
    end;
    From the Example above, I call a program based on user choice. How Can I do the same in Java?
    Thank you.
    Edited by: Eric S. on Jul 6, 2011 9:52 AM

    I have a requirement to call a Class or a MethodYou can call a method, but not the Class where as you can instantiate the Class.
    I will give an example with how I do it in pl sql.
    begin
    if (choice = 1) then
    execute immediate 'package.procedure1(params)';
    elsif (choice = 2) then
    execute immediate 'package.procedure2(params)';
    else
    execute immediate 'package.procedure3(params)';
    end if;
    end;
    From the Example above, I call a program based on user choice. How Can I do the same in Java?The similar way. Here it takes the following structure.
    ClassA object = new ClassA();
    if(choice==1) {
          object.callMethod1();
    } else if(choice==2) {
          object.callMethod2();
    } else if(choice==3) {
          object.callMethod3();
    } Also you can switch case instead of if else. It takes the following structure.
    ClassA object = new ClassA();
    switch(choice) {
    case 1:  object.callMethod1(); break;
    case 2:  object.callMethod2(); break;
    case 3:  object.callMethod3(); break;
    }I recommend you to go with the second one which using switch.

  • How to declare a dynamic array in java

    I have to use a dynmic array of boolean and the size of this array will defiend throuh the run of the program to explian
    boolean[][] a;
    a=new boolean[number_of_ raw][number_of_ culm];
    after the program run the number_of_ raw and number_of_ culm will defind..so if any body know how to declare this kind of arraies in java please help me.

    My previous post gives me an idea on how to use ArrayList of ArrayList as dynamic 2 dimensional array.
    More info bellow
    ArrayList rows = new ArrayList()
    ArrayList columns_of_row0 = new ArrayList()
    rows.insertAt(0) = columns_of_row0;
    ArrayList columns_of_row1 = new ArrayList()
    rows.insertAt(1) = columns_of_row1;and so on..
    You can't use the code exactly or similar way, it would depend on how this array grows in your application.
    But this and my previous post should give an idea on how to insert and retrieve elements from ArrayList when
    treated as 2 dimensional array.
    Hope this helps.

  • Java Mapping : Multimapping possible or not?

    Hi Experts ,
    Can we use Java Mapping in Case of Multimapping where I am getting 2 messages as Input and I have to sent 1 message O/p.
    Also the reverse case where I am getting I message as I/p and I have to sent 2 messages O/p
    Your suggestions are required .
    Regards

    Hi Amit,
    It is possible using Java Mapping.
    Also, go through the below links to get a better idea.
    http://help.sap.com/saphelp_nw04/helpdata/en/43/09b16006526e72e10000000a422035/frameset.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/42/f3ca2529491bc7e10000000a11466f/frameset.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/42/f3b31d48fb1bc8e10000000a11466f/frameset.htm
    /people/thorsten.nordholmsbirk/blog/2006/08/10/using-jaxp-to-both-parse-and-emit-xml-in-xi-java-mapping-programs
    /people/amol.joshi2/blog/2006/03/10/think-objects-when-creating-java-mappings
    Structure Mapping: suppress multi occurence based on field content
    Hope these help
    Regards
    Kiran..

Maybe you are looking for

  • Can not resume from blanking or screensavers

    Hello, I have filed Bug #21395, with no response or assignment of the bug since I've filed it. My hardware goes as follows: Intel P4 Pro, 2.4-Ghz processor desktop PC 1 Gb RAM Nvidia 7300 GT card Audigy 2 sound card The issue with the bug has not dec

  • Batch Creation without classification

    Hi all, I have activated batch management for Material A, When i do MIGO system is asking for Batch i am also entering some Batch say 1023 But i have not gone to MSC1N where we create Batch for particular material. without creating there, i directly

  • X100e regularly dropping wireless connection

    Had my new x100e (Windows 7 Home Premium, 2GB RAM, 22GB hard drive) about 3 weeks now- trouble is it drops the wireless connection consistently and regularly every three minutes- I've sat and timed it.  Then it will immediately start reconnecting.  H

  • ITunes Library sharing error -3259

    Hi I am trying to share my iTunes library stored on my iMac (Leopard) with a mac mini (10.4.11). The library name appears in the shared list on the MacMini iTunes but when I click it, it doesn't open up, just hangs until I get an error message saying

  • Why am i getting this NullpointerException?

    i have an applet in which there are panels. i have a subclass panel which extends a superclass panel. in the superclass, there is an instance of a different panel. when i try to call the getX() method of the different panel in the subclass, it gives