Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Creating Method that returns a casted object type

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,628
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Creating Method that returns a casted object type

    Ok, I am attempting to do something a bit odd. Let's say I have the following classes:
    Class Super:
    public class Super {
    ...
    }
    Class SubA:
    public class SubA extends Super {
    ...
    }
    Class SubB:
    public class SubB extends Super {
    ...
    }

    I also have the following classes:
    Class Helper:
    public class Helper {
         private Super var;
         private Class<? extends Super> type;
         public void setUp(Class<? extends Super> ty) {
              type = ty;
              var = type.newInstance();
         }
         public ???? getSuper() {
              return type.cast(var);
         }
    }
    Class SubATest:
    public class SubATest extends Helper {
         public void setUpTest() {
              setUp(SubA.class);
         }
    }
    Class SubBTest:
    public class SubBTest extends Helper {
         public void setUpTest() {
              setUp(SubB.class);
         }
    }

    Now, Helper is a class that will be used by the various classes which test subclasses of Super. In order to perform various operations, Helper needs access to the subclass variable of Super which is being tested on, so Helper contains a Super object: var, as well as a reference to what subclass var is supposed to be.
    I am attempting to create a method which returns var, casted into its intended subclass (getSuper() method), but I am unsure what the return type should be. If I set the return type as a Super object, it will come back uncasted, which defeates the entire purpose of doing this.

    My first question is if this is even possible to do. And my second question would be what return type I need to use.

    Any help is appreciated.
    Last edited by aussiemcgr; October 31st, 2012 at 12:42 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Creating Method that returns a casted object type

    You could think about adding generics to your Helper class. Something like this:

    class Helper<S extends Super>{
     public S getSuper() {
              return type.cast(var);
         }
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,628
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Creating Method that returns a casted object type

    Generics...why didn't I think of that...?

    That solves just about every issue. Thanks.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Creating object everytime object is called
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2012, 04:18 PM
  2. Replies: 1
    Last Post: February 12th, 2012, 12:01 AM
  3. [SOLVED] isPrime Boolean Method returns true for squares of primes
    By Staticity in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 29th, 2011, 11:46 PM
  4. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  5. Creating array from generic type objects?
    By AndrewMiller in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2010, 08:22 PM