Results 1 to 4 of 4
- 07-01-2011, 05:32 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 1
- Rep Power
- 0
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
- 07-01-2011, 06:11 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
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.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 may help you implementing what you want. You can however make it take a generic argument.Last edited by sunde887; 07-01-2011 at 06:15 AM.
- 07-01-2011, 06:13 AM #3
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:
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)Java Code:if (obj instanceof SubObject) { doThat; }
EDIT: BLAST! Beaten by sunde. :\
- 07-01-2011, 06:19 AM #4
Similar Threads
-
generic parameters
By TopNFalvors in forum New To JavaReplies: 5Last Post: 03-18-2011, 08:46 PM -
Generic Question
By kwgivler in forum New To JavaReplies: 5Last Post: 03-10-2011, 10:50 PM -
Generic question on implementing bluetooth/net code
By SM2010 in forum New To JavaReplies: 2Last Post: 12-14-2010, 04:10 PM -
how to pass parameters from a method to another which accepts to parameters?possible?
By amrmb09 in forum Advanced JavaReplies: 5Last Post: 11-21-2010, 02:08 PM -
Java Generic Container Question - please help!!!
By zhoujackji in forum New To JavaReplies: 3Last Post: 11-13-2010, 11:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks