Results 1 to 4 of 4
  1. #1
    Dirty Protest is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default Question about generic parameters

    Hello, I have 5 different classes which compare 2 Strings in different ways . I was wondering is its possible to have a class which uses Object in its constructor argument, then runs the compare method of the appropriate class which was passed. So something like this;

    public compare(Object obj)
    {
    If(obj = thisClass)
    {
    doThat;
    }
    If(obj = thatClass)
    {
    doThis;
    }
    }

    Thanks

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How does this have to do with generics? Or do you mean referring to an object generically via a superclass or interface?

    Also, is this pseudo code? Because you are using assignment operators on your ifs and not comparison(==), also, you may not want to use the comparison operator.

    Just to show you something I will write a class which overrides equals. The overridden equals method take an Object arg

    Java Code:
    public class X{
      private int x;
      private String s;
    
      @Override public boolean equals(Object o){
        if(o == this)
          return true;
        if(!(o instanceof X))
          return false;
        X x = (X) o;
        return x.x == x && x.s.equals(s);
      }
    }
    This does a few things. Checks if o is the same as the object the method being called. If you call o.equals(o); it returns true because of the first if statements. Next if the object argument and the object calling equals are not the same it checks if o is an instance of type X, if it isn't you can turn false(an object of String can never be equals to an Integer object), if you make it past this you know the object is of type X and you can finally cast it to type X and perform comparisons.

    This may help you implementing what you want. You can however make it take a generic argument.
    Last edited by sunde887; 07-01-2011 at 07:15 AM.

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    As far as I know, the obj you provide has to always be a subclass of Object. Then you would do something like this, assuming that class SubObject extends Object:
    Java Code:
    if (obj instanceof SubObject) { doThat; }
    If they do not extend the same class, you would have to use generic types... Generic Types (The Java™ Tutorials > Learning the Java Language > Generics)

    EDIT: BLAST! Beaten by sunde. :\

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Are you saying that you have classes A B C D & E. Inside class A you want a compare method that can take an object of any of those 5 classes and then compare then. If so, why? Why would you want to compare a Dog with a Chair or a Fork or a Bottle etc?

Similar Threads

  1. generic parameters
    By TopNFalvors in forum New To Java
    Replies: 5
    Last Post: 03-18-2011, 09:46 PM
  2. Generic Question
    By kwgivler in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 11:50 PM
  3. Replies: 2
    Last Post: 12-14-2010, 05:10 PM
  4. Replies: 5
    Last Post: 11-21-2010, 03:08 PM
  5. Java Generic Container Question - please help!!!
    By zhoujackji in forum New To Java
    Replies: 3
    Last Post: 11-13-2010, 12:12 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
  •