Results 1 to 7 of 7
  1. #1
    plot is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Passing a unknown type to a method and returning it

    So I want to know how in Java you can pass a unkown type into a method (type can be an int, double, or a user defined object) and return that unkown type.

    example of what I want:
    Java Code:
    public (unknowntype)[] method2 ((unknowntype)[])
    {
        //Process Data
        //unknowntype.process();
        return (unknowntype);
    }

    I know in C you can use void pointers and in c++ we have templates but I do not know how java handles this. Also I want to know if it is possible to call a method in the unknowntype.

    Thank you

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Passing a unknown type to a method and returning it

    Can you explain why you want to do that? Can method overloading handle most of what you are trying to do?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Passing a unknown type to a method and returning it

    In general, you should write your methods to take the least specific type that has the relevant properties. If you have a class called Super and two subclasses called Sub1 and Sub2, you would write your method to take an argument of type Super. Then you could pass it a Sub1, a Sub2, or any subtype yet to be written. This is the essence of polymorphism.

    The most "unknown" argument of all is type Object. There are times when you would want to take an Object, but there isn't much you can do with it. The only methods you can call on the argument without resorting to trickery are those defined by Object.
    Get in the habit of using standard Java naming conventions!

  4. #4
    plot is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: Passing a unknown type to a method and returning it

    Quote Originally Posted by kjkrum View Post
    In general, you should write your methods to take the least specific type that has the relevant properties. If you have a class called Super and two subclasses called Sub1 and Sub2, you would write your method to take an argument of type Super. Then you could pass it a Sub1, a Sub2, or any subtype yet to be written. This is the essence of polymorphism.

    The most "unknown" argument of all is type Object. There are times when you would want to take an Object, but there isn't much you can do with it. The only methods you can call on the argument without resorting to trickery are those defined by Object.
    cant you store an object in stacks and binary trees? you can insert an object into a stack and then the stack will return that object when you pop it out, I know in C you can handle unknown types by using a void* then have a pointer to function that will process that type.

    In java you use inheritance for code reuseability however the method you are suggesting is not very good since it is not reusable and everytime I create an object I would have to always override that method and there would not be an instance were I can reuse that code for another object. There has to be a way were java handles any type or user defined object since I know stacks exist in java and stacks do handle any object pushed in and popped out.

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Passing a unknown type to a method and returning it

    Sure, you can store a Collection of Objects and then cast them into whatever type you think they're supposed to be. But that will turn compile-time errors into run-time errors.

    Java does not have function pointers. They are simulated with function objects, which are basically objects that implement an interface that has exactly one method. It sounds like that's the pattern you're looking for. Your method argument would not be a totally unknown type, but an unknown implementation of your function object interface.
    Get in the habit of using standard Java naming conventions!

  6. #6
    plot is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: Passing a unknown type to a method and returning it

    Quote Originally Posted by kjkrum View Post
    Sure, you can store a Collection of Objects and then cast them into whatever type you think they're supposed to be. But that will turn compile-time errors into run-time errors.

    Java does not have function pointers. They are simulated with function objects, which are basically objects that implement an interface that has exactly one method. It sounds like that's the pattern you're looking for. Your method argument would not be a totally unknown type, but an unknown implementation of your function object interface.

    You do something like this

    class test<Item>
    {
    method1(Item t)
    {
    //Do what you need to here
    }
    }

    This is how a method can take any object including userdefined objects, now I need to know how to process information from using a method in that object.

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Passing a unknown type to a method and returning it

    Are you asking how to write classes that use generics?

    Even if you use generics, you still can't write a method that takes any object and calls a method on it, unless it's a method that exists in class Object. At least not with compile-time type safety. And your method won't be able to accept primitives; you'll have to use their boxed values.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Unknown type of an interface field
    By vallach in forum New To Java
    Replies: 2
    Last Post: 11-27-2012, 09:49 AM
  2. What Should be my Reurn type for returning a file
    By Anagha in forum New To Java
    Replies: 2
    Last Post: 04-13-2011, 12:56 PM
  3. passing type info to a method during run time
    By alinaqvi90 in forum Advanced Java
    Replies: 2
    Last Post: 10-20-2010, 02:03 PM
  4. Polygons... returning and passing objects?
    By asherwolf in forum New To Java
    Replies: 3
    Last Post: 07-09-2010, 04:41 PM
  5. JNI - passing and returning parameters by value
    By java_to_c in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 08:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •