Parent class and Casting

Hi all,
I need to store different classes in a Vector. The classes have in common that they are all different views (MVC!). If I make a common parent, can I cast to that parent without problems?
Background:
I'm programming on a mobile, which uses different screens (choice screen, data screen and help screen). Depending on an event, be it a button pressed, or data comming in from another mobile, the view the player gets needs to be changed. The Model will call an update () method on the view. If I make the update method abstract, the update method of the child class will be used?
Maik

I need to store different classes in a Vector. The
classes have in common that they are all different
views (MVC!). If I make a common parent, can I cast
to that parent without problems?Yes.
If I make the update
method abstract, the update method of the child class
will be used? Is there any other? It will always use the method the instance implements.

Similar Messages

  • Get runtime class and cast object to it?

    Hi,
    public interface Msg
    Msg msg=msgLib.get(msgType);
    Msg msg2=msg.clone(); //problem! cannot find clone in  x.y.Msg!
    ...Shouldn't msg be resolved to the implementing class (MsgReply for example) and use the "clone()" method, that i have declared in that class -which overrides the one of Object class?
    If no, is there a way to find the runtime class -probably use "getClass"- and cast to it? How?
    Thanks

    There are many implementing classes; i want to get the runtime class. Something like:
    (msg.getClass())msg.clone();
    Message was edited by:
    uig

  • Seemingly unpredictable results when calling an overriden parent method on an instance of a child class casted to the parent class

    I have a parent class with a sub-vi Override.vi, and a child which overrides this sub-vi.  I create an instance of this child.  I cast this child to it's parent class and store it in an array.  Later, if I invoke the parent's 'Override.vi' on this child (casted to parent) then Labview 2013 seems to randomly choose whether to run the parent or the child override.vi.  In Labview 2011 SP1 it would always call the childs version of override.vi (which while surprising to me was very useful).  This has totally broken an application I have been developing, any insight as to how to control which override.vi is run would be helpful (re-casting to the child class isn't really an option, as there are in fact many child classes each with their own version of override.vi).

    The actual data type of the wire is irrelevant in deciding which VI to run. The only thing that is relevant is the class of the object which is actually on the wire, so casting to the parent should not be relevant. *IF* the object really is a child, then LV should always call the child's VI, just like you say it works in 2011.
    I suspect that what's happening in your case is that somewhere you're generating a parent and that's what's actually on the wire (e.g. maybe you have an error somewhere and a function outputs the default value, which is a parent). The fact that it didn't happen in 2011 doesn't mean it's a bug in 2013. It could be that something else has changed.
    In any case, it's impossible to tell whether this is a misunderstanding, a bug in your code or a bug in LV without actual code. If you can post actual code which shows this, people can help. Otherwise (if it only happens in code you don't want to publish), you should try contacting NI directly so that you can at least show them the code.
    Try to take over the world!

  • Question about inheritance and  casting

    according to the java api it said Calendar() is a super-class of GregorianCalendar(). with that said it means GC inherits all members of C(). i hope everyone agrees with me here.
    why is it necessary to do this:
    Calendar c = new GregorianCalendar();//by the way this is called implicit casting per the java tutorial.if GC inherits everything from C, and if you need members from both classes, then wouldn't it be enough to just do
    GregorianCalendar gc = new GregorianCalendar();why is it necessary to declare a variable of the parent class and assign a subclass reference to that variable?

    then why does the tutorial show this?Why not? It's legal Java. It's what I would probably write, unless I knew I was going to use some methods that were in GregorianCalendar and not in Calendar.

  • How can I casting from parent class to children class

    Dear,
    Could someone help me to casting from parent class to children class.
    I have class like this
    class parent{
    String name;
    String id;
    public String getId() {
    return id;
    public void setId(String id) {
    this.id = id;
    public String getName() {
    return name;
    public void setName(String name) {
    this.name = name;
    class children extends parent{
    String address;
    public String getAddress() {
    return address;
    public void setAddress(String address) {
    this.address = address;
    public children() {
    public children(parent p) {
    //Do init super class here
    In the constructor
    public children(parent p) {
    //Do init super class here
    I like to init super class by object p (this is instance of parent class). The way to do is using:
    public children(parent p) {
    super.setId(p.getId());
    super.setName(p.getName());
    But I don't like this, because, for example I have parent class with over 30 proberties, it take time to do like that.
    There are any way to use super operation to init parent class, for example super = p;
    Could you show me the way.
    Thanks alot

    If I understand your question correctly, you are in need of a copy constructor for you class Parent. A copy constructor behaves like this:
       Parent one = new Parent();
       one.setName("...");
       //... and all other properties of interest
       Parent two = new Parent(one);
       //Now two != one, but one.getName().equals(two.getName) for property name and all othersThe copy constructor is programmed in the Parent class, more later. Then for your child class you can use it as follows
       public class Children extends Parent {
           public Children(Parent p) {
              super(p);
       }There are at least 3 ways of programming a copy constructor:
    1. Just bite the bullet: type the assignment for each field this.name = p.getName()
    2. Use reflection to find all common setters/getters dynamically and assign using them
    3. Use a code generator that uses 2 to give you the code for solution 1 for you to paste in.
    If you find doing this a lot, there are frameworks that can do these mappings, like Dozer
    (PS be carefull with Date fields, don't copy the reference but create a new and equals instance, the dirty way would be this.birthdate = new Date(p.getBirthdate.getTime()); )

  • Accessing subclass data when parent class is part of private data of another class.

    I am working up an OOP test program and have a problem with data access.
    I have the following classes defined:
    General Test
    General Report
    Specialized Test
    Specialized Report
    The Specialized Test and Specialized Report are subclasses of General Test and General Report, respectively.
    The General Test class contains a General Report class instance as part of it's private data.
    Specialized Test writes a Specialized Report to its parent class data on initialization. The Specialized Report contains additional data items in its private data.
    I have a method for Specialized Test where I first access the Report object stored in the General Test private data, then try to access the private data of the Specialized Report, that should be returned.I get a class mismatch, as the General Test accessor does not know it is returning a Specialized Report.
    Should I simply typecast the returned report to the more specific class before trying to access the subclass data as shown below, even though it will already be the more specific class?
    Thanks,
    Josh

    I don't see your image - maybe it just hasn't finished uploading yet - but if I'm understanding correctly, using To More Specific Class (not Type Cast) is a fine solution here. It's reasonable to use To More Specific Class to get the wire type to match the type of data that you know it will carry when the compiler cannot make that determination for you automatically.

  • Class override, how to create the child class and then the base class

    I started to write a program for a smart DMM, the problem is every version of the DMM the company change the communication commend.
    My idea is to write a child class for every DMM version and every SubVI of the child will override the base class SubVI.
    My problem is, i want first to create one child class and after i will see every thing is work,  start to create the base class. that way i will see if am thinking the right way.
    My question is
    How can i create a child class and then create the base class and configure the SubVi of the child class to be Override of the base class?
    I tried to search in the property of the class but i didn't see nothing.
    Thanks
    Solved!
    Go to Solution.

    This can be done and I've done it on occasion.
    You simply create the base class with the dynamic dispatch methods you require (connector panes need to be identical to thos of the child class).
    Then set the inheritance of the class to inherit from this base class.  If your method is defined as a dynamic dispatch method in the parent, you'll most likely now have some errors (unless your child method was already DD in which case you might just be OK already).
    To change the inheritance of a class, right-click the properties of the class in your project and select properties.  I believe the ineritance tree is at the lower end of the properties.  Click on the "change inheritance" (or something similar) to choose the class from which you now wish to inherit.
    Say hello to my little friend.
    RFC 2323 FHE-Compliant

  • SQL Server 2012 Management Studio:In the Database, how to print out or export the old 3 dbo Tables that were created manually and they have a relationship for 1 Parent table and 2 Child tables?How to handle this relationship in creating a new XML Schema?

    Hi all,
    Long time ago, I manually created a Database (APGriMMRP) and 3 Tables (dbo.Table_1_XYcoordinates, dbo.Table_2_Soil, and dbo.Table_3_Water) in my SQL Server 2012 Management Studio (SSMS2012). The dbo.Table_1_XYcoordinates has the following columns: file_id,
    Pt_ID, X, Y, Z, sample_id, Boring. The dbo.Table_2_Soil has the following columns: Boring, sample_date, sample_id, Unit, Arsenic, Chromium, Lead. The dbo.Table_3_Water has the following columns: Boring, sample_date, sample_id, Unit, Benzene, Ethylbenzene,
    Pyrene. The dbo.Table_1_XYcoordinates is a Parent Table. The dbo.Table_2_Soil and the dbo.Table_3_Water are 2 Child Tables. The sample_id is key link for the relationship between the Parent Table and the Child Tables.
    Problem #1) How can I print out or export these 3 dbo Tables?
    Problem #2) If I right-click on the dbo Table, I see "Start PowerShell" and click on it. I get the following error messages: Warning: Failed to load the 'SQLAS' extension: An exception occurred in SMO while trying to manage a service. 
    --> Failed to retrieve data for this request. --> Invalid class.  Warning: Could not obtain SQL Server Service information. An attemp to connect to WMI on 'NAB-WK-02657306' failed with the following error: An exception occurred in SMO while trying
    to manage a service. --> Failed to retrieve data for this request. --> Invalid class.  .... PS SQLSERVER:\SQL\NAB-WK-02657306\SQLEXPRESS\Databases\APGriMMRP\Table_1_XYcoordinates>   What causes this set of error messages? How can
    I get this problem fixed in my PC that is an end user of the Windows 7 LAN System? Note: I don't have the regular version of Microsoft Visual Studio 2012 in my PC. I just have the Microsoft 2012 Shell (Integrated) program in my PC.
    Problem #3: I plan to create an XML Schema Collection in the "APGriMMRP" database for the Parent Table and the Child Tables. How can I handle the relationship between the Parent Table and the Child Table in the XML Schema Collection?
    Problem #4: I plan to extract some results/data from the Parent Table and the Child Table by using XQuery. What kind of JOIN (Left or Right JOIN) should I use in the XQuerying?
    Please kindly help, answer my questions, and advise me how to resolve these 4 problems.
    Thanks in advance,
    Scott Chang    

    In the future, I would recommend you to post your questions one by one, and to the appropriate forum. Of your questions it is really only #3 that fits into this forum. (And that is the one I will not answer, because I have worked very little with XSD.)
    1) Not sure what you mean with "print" or "export", but when you right-click a database, you can select Tasks from the context menu and in this submenu you find "Export data".
    2) I don't know why you get that error, but any particular reason you want to run PowerShell?
    4) If you have tables, you query them with SQL, not XQuery. XQuery is when you query XML documents, but left and right joins are SQL things. There are no joins in XQuery.
    As for left/right join, notice that these two are equivalent:
    SELECT ...
    FROM   a LEFT JOIN b ON a.col = b.col
    SELECT ...
    FROM   b RIGHT JOIN a ON a.col = b.col
    But please never use RIGHT JOIN - it gives me a headache!
    There is nothing that says that you should use any of the other. In fact, if you are returning rows from parent and child, I would expect an inner join, unless you want to cater for parents without children.
    Here is an example where you can study the different join types and how they behave:
    CREATE TABLE apple (a int         NOT NULL PRIMARY KEY,
                        b varchar(23) NOT NULL)
    INSERT apple(a, b)
       VALUES(1, 'Granny Smith'),
             (2, 'Gloster'),
             (4, 'Ingrid-Marie'),
             (5, 'Milenga')
    CREATE TABLE orange(c int        NOT NULL PRIMARY KEY,
                        d varchar(23) NOT NULL)
    INSERT orange(c, d)
       VALUES(1, 'Agent'),
             (3, 'Netherlands'),
             (4, 'Revolution')
    SELECT a, b, c, d
    FROM   apple
    CROSS  JOIN orange
    SELECT a, b, c, d
    FROM   apple
    INNER  JOIN orange ON apple.a = orange.c
    SELECT a, b, c, d
    FROM   apple
    LEFT   OUTER JOIN orange ON apple.a = orange.c
    SELECT a, b, c, d
    FROM   apple
    RIGHT  OUTER JOIN orange ON apple.a = orange.c
    SELECT a, b, c, d
    FROM   apple
    FULL OUTER JOIN orange ON apple.a = orange.c
    go
    DROP TABLE apple, orange
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Want to find out parent class of a class without using getSuperClass

    hi,
    I am trying to write a code to find out the base class /interfaces of a class by its object and i dont want to use getSuperClass,getInterfaces methods.
    Below is a snippet i tried but it does not work:
    public class Test extends Parent{
    public void disp(){
    System.out.println(super.getClass().getName());
    public static void main(String args[]){
    Test test = new Test();
    test.disp2();
    this outputs the child class only does not output the parent class.
    Is there any method to do so?
    Thanks,
    Anirudh

    Ok, If it can not be done then i must not ask.
    So you have answered it that it is not possible without these.Thats what i wanted to know.
    Should have framed the question like is it possible?and if possible how?
    Thanks
    Anirudh

  • How to get the class name  static method which exists in the parent class

    Hi,
    How to know the name of the class or reference to instance of the class with in the main/static method
    in the below example
    class AbstA
    public static void main(String[] args)
    System.out.println(getXXClass().getName());
    public class A extends AbstA
    public class B extends AbstA
    on compile all the class and run of
    java A
    should print A as the name
    java B
    should print B as the name
    Are there any suggestions to know the class name in the static method, which is in the parent class.
    Regards,
    Raja Nagendra Kumar

    Well, there's a hack you can use, but if you think you need it,Could you let me the hack solution for this..
    you probably have a design flaw and/or a misunderstanding about how to use Java.)May be, but my needs seems to be very genuine..of not repeat the main method contents in every inherited class..
    The need we have is this
    I have the test cases inheriting from common base class.
    When the main method of the test class is run, it is supposed to find all other test cases, which belong to same package and subpackages and create a entire suite and run the entire suite.
    In the above need of the logic we wrote in the main method could handle any class provided it knows what is the child class from which this main is called.
    I applicate your inputs on a better way to design without replicating the code..
    In my view getClass() should have been static as the instance it returns is one for all its instances of that class.
    I know there are complications the way compiler handles static vars and methods.. May be there is a need for OO principals to advance..
    Regards,
    Raja Nagendra Kumar
    Edited by: rajanag on Jul 26, 2009 6:03 PM

  • My derived class suddenly doesn't recognize the parent class

    I had my assignment coded, debugged, working and ready to go. So I bring my flash drive upstairs to play around with it on my other computer. When I compile the base class it compiles fine, but now when I try to compile the derived class, and the test class with the main method, I'm coming up with errors.
    They have something to do with the base class not being recognized, because I can't instantiate any objects of the class now, and i get an error when attempting to compile the derived class to the effect of "cannot find symbol; symbol: class BaseClass". i get this error throughout the derived class, from the initial declaration of the class (public class DerivedClass extends BaseClass), to any references made toward methods of the parent class.
    My base class compiles with no problem, but it's like nothing else is able to "use" it. I'm not sure if it has something to do with the PC im using now, and I can't confirm this at the moment because the PC I used to write the code is down (waiting on an upgrade).
    ehhh.....help.
    Message was edited by:
    asphaltninja
    null

    It's not the fault of the computer hardware.
    It's not the fault of the operating system.
    It's not the fault of the JDK.
    So don't go rebooting or reinstalling anything. The problem is that you didn't set up things on the new computer in the same way they were set up on the old computer.
    In particular the compiling problem is that the compiled version of BaseClass isn't in the classpath when you try to compile DerivedClass. I can't tell why from your description.

  • Reflection: how to get the name of a subclass from parent class?

    Suppose I have a parent class P and two subclasses S1, and S2. There's another method which has an argument of type P. Inside this method, I want to inspect the object (of type P) passed in and print its name, such as "S1" or "S2". How do you do that? I tried Class.getSimpleName(), but "P" is returned no matter which subclass objects you have. Thanks:)

    That's the same as you said last time, and I'm telling you that's not what happens when I test it:
    public class Parent {
    public class SubclassOne extends Parent {
    public class SubclassTwo extends Parent {
    public class TestGetName {
      public static void main(String[] argv) {
        showNames(new SubclassOne());
        showNames(new SubclassTwo());
        showNames(new Parent());
      private static void showNames(Parent p) {
        System.out.println("Name: " + p.getClass().getName());
        System.out.println("Simple name: " + p.getClass().getSimpleName());
        System.out.println("Canonical name: " + p.getClass().getCanonicalName());
    }prints:
    Name: SubclassOne
    Simple name: SubclassOne
    Canonical name: SubclassOne
    Name: SubclassTwo
    Simple name: SubclassTwo
    Canonical name: SubclassTwo
    Name: Parent
    Simple name: Parent
    Canonical name: ParentYou must be doing something else that you're not saying. Either that or you're expressing yourself very poorly. Why not post a simple, self-contained, compilable example of what you claim is happening?

  • How to access the parent class variable or object in java

    Hi Gurus,
    I encounter an issue when try to refer to parent class variable or object instance in java.
    The issue is when the child class reside in different location from the parent class as shown below.
    - ClassA and ClassB are reside in xxx.oracle.apps.inv.mo.server;
    - Derived is reside in xxx.oracle.apps.inv.mo.server.test;
    Let say ClassA and ClassB are the base / seeded class and can not be modified. How can i refer to the variable or object instance of ClassA and ClassB inside Derived class.
    package xxx.oracle.apps.inv.mo.server;
    public class ClassA {
        public int i=10;
    package xxx.oracle.apps.inv.mo.server;
    public class ClassB extends ClassA{
        int i=20;   
    package xxx.oracle.apps.inv.mo.server.test;
    import xxx.oracle.apps.inv.mo.server.ClassA;
    import xxx.oracle.apps.inv.mo.server.ClassB;
    public class Derived extends ClassB {
        int i=30;
        public Derived() {
           System.out.println(this.i);                  // this will print 30
           System.out.println(((ClassB)this).i);  // error, but this will print 20 if Derived class located in the same location as ClassB
           System.out.println(((ClassA)this).i);  // error, but this will print 20 if Derived class located in the same location as ClassA
        public static void main(String[] args) { 
            Derived d = new Derived(); 
    Many thanks in advance,
    Fendy

    Hi ,
    You cannot  access the controller attribute instead create an instance of the controller class and access the attribute in the set method
    OR create a static method X in the controller class and store the value in that method. and you can access the attribute by 
    Call method class=>X
    OR if the attribute is static you can access by classname=>attribute.
    Regards,
    Gangadhar.S
    Edited by: gangadhar rao on Mar 10, 2011 6:56 AM

  • How can a custom class call a function in "parent" class?

    Say I have an application (ultrasimplified):
    public class myApp {
    myClass mc;
    boolean foo=false;
    public static void main(String[] args) {
    mc = new myClass();
    public static void myFunc(boolean blah) {
    foo=blah;
    in a separate .java file where my questions lie:
    public class myClass {
    boolean bar=true;
    public void myClass() {
    // this is wrong, but how would I do this:
    foo = bar; // foo in myApp set to true
    // or how would I call "myFunc()" in myApp from this class:
    myFunc(bar);
    my problem is that I've created a new class that I share between two applications so I could share the code. However, I want this class I created to call a function in the application class that instanciated it. Or alternatively, I would like to set a variable in the class that instanciated myClass.
    How would go about this? I've used the "this" parameter in applets to pass the parent class to an inner class, but main() in applications doesn't allow the non-static "this":
    myClass my = new myClass(this);
    Is there something similar I can do?

    You can let MyApp implement an interface and refer to that object in MyClass:
    class MyApp implements Something {
    main() {
    MyApp app = new MyApp();
    MyClass mc = new MyClass(app);
    public void foo() { }
    class MyClass {
    Something app;
    MyClass(Something app) {
    this.app = app;
    app.foo();
    interface Something {
    public void foo();
    Better yet, you can let MyApp extend an abstract class that defines foo(). Then MyApp can override the foo() method. If later on, the abstract class needs to add a bar() method then default implementation can be done in the abstract class. If you make it an interface then all implementing classes will have to be updated to implement the new method.

  • Accessing variables in a parent class?

    Hello everyone,
    I'm rather new to java, and I've been looking at the documentation a bit but am still trying to figure out just what I am looking for. I've posted an example of such before where I have two seperate files, the parent class file and the child class file. What I want to do is somehow return the instance of parent that child was created with from the below code.
    public class parent {
         protected int var;
         public static void main(String[] args) {          
              child chld = new child();
    }Is it possible to get the same instance of parent that created the child? If so, can it be used to access or modify the variables held by parent? Or is it only possible to do this by creating methods to set or get the value of the variable?
    Can someone point me to the proper term as well, so as I might check the manual as well?

    Another thing I just noticed; your question does not seem to match your post's subject title. Taking that into context, it is possible to read your post very differently. Do you mean that you want to reference parent from inside of its own method, not necessarily after it has returned?
    You can access an object's variables and its methods from within a method it owns. To clarify what I mean...
    public class Something
        int someData = 0;
        public void someAction()
            // Something has a variable "someData" which can be accessed directly from inside of Something's own method someAction()
            someData = 5;
            System.out.println(someData);
    }If you mean you want a reference to the object itself from within one of its methods, Google for the "this" keyword.
    None of these have anything to do with the child object though, so I'm still a bit stumped as to what exactly you mean. Perhaps a combination of all answers received so far?
    Again, you probably need to further clarify exactly what you're looking for.

Maybe you are looking for

  • Error Messages in FPM

    hi, How to trigger error messages in FPM ? I have added one custom field in Standard Travel Component and have to validate the same. Error messages with Focus on Attribute has to be done.

  • Outofmemory while creating a new object of file with size more than 100 MB

    I have created an application which generates a report by getting the data from our archived files (.zip file).By the time, the application is reaching a file with size more than 100 mb, it is running out fo memory while creating the object of that p

  • Java API to find group members?

    I need to find a way to list all the members of a group. There're only two similar methods in the oracle OID java API. Group.isMember() and Util.getGroupMemberShip(). Is there something like Group.getGroupMembers(), or Util.getGroupMembers()? If not,

  • Qaaws is not showing all the universes

    Hi All, In my BO Box their were around 400 universes are there. when I am trying to create the New Qaaws Services using QAAWS tool I am able to see the only few Universes whose names are starting with a to f, I am not able to see the universe whose n

  • Capturing DDL only for 'ADDING and DELETING' a column

    Hi Source Oracle 11.2 GG Version: 11.2 I am configuring the replication between oracle-oracle and regarding DDL replication ,for few tables , in addition to the DML replication , i want to capture only the DDL's related to the structure changes to th