Results 1 to 12 of 12
Thread: super instanceof Class?
- 01-23-2009, 01:58 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
- 01-23-2009, 02:02 AM #2
- 01-23-2009, 02:54 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
that doesn't seem to work with the super keyword
- 01-23-2009, 03:05 AM #4
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
I've seemed to have found a work around which uses a helper class.
If anyone one knows the answer to the original question, I'd be interested to know how. Thanks.
-
It kind of begs the question: why do you want to do this? It smells of design problems.
- 01-23-2009, 04:10 AM #6
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
I'm trying to create a series of filters for data. For example (types of data filters and their hierarchy):
.................[TCP]..................
........[Web]..........[E-mail]......
[images]..[html]..[pop]...[smtp]
So it seemed logical to create an Abstract filter class:
This way it would be much easier to create a TreeMap of the filters and sort the tree accordingly.Java Code:public abstract class Filter2{ public final boolean isTrue(Packet p){ if(super instanceof Filter2){ //and super is not equal to filter return evaluate(p) && super.isTrue(p); } } public abstract boolean evaluate(Packet p); public final int getFilterLevel(){ if(super instanceof Filter2){ //and super is not equal to filter return 1+super.getFilterLevel(); } } public final LinkedList<Class> getFilterLevels(){ // not sure how useful this method really is if(super instanceof Filter2){ //and super is not equal to filter LinkedList<Class> l = super.getFilterLevels(); l.add(super.getClass()); return l; }else{ return new LinkedList<Class>(); } } }
Is there a better/easier way to do this?Last edited by mikeiz404; 01-23-2009 at 04:12 AM. Reason: removed extends FilterHelper
- 01-23-2009, 05:48 AM #7
=\ no idea what your code does. but you can use the class Class. <= no thats not a typo.
you can put that in a method to make it easier to read.Java Code:if(this.getClass().getSuperclass().getSimpleName().equals("Filter2")) ...USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 01-23-2009, 08:20 AM #8
Shorter:
dbJava Code:if(this.getClass().getSuperclass() == Filter2.class) ...
- 01-23-2009, 04:48 PM #9
I see what you are up to. This is not a good approach.
If you want to determine the type of an object, just use something like (assuming Filter2 is the base of your hierarchy)
This will return the fully qualified name of the class, like java.lang.String. You can then use String.startsWith() to see if it is part of a particular hierarchy.Java Code:public String getClassType(final Filter2 pInstance) { final String className; className = pInstance.getClass.getName(); return className; }
- 01-23-2009, 05:54 PM #10
- 01-23-2009, 07:17 PM #11
The main reason to use "super" is to access overridden methods of the base class.
If "super instanceof SomeClass" is true, then "this instanceof SomeClass" must also be true. The same holds for false. The only exception is if we replace SomeClass with SomeInterface. This discussion is part of the reason Java only allows single inheritance.
There is no need to resort to "super".
Be careful of getClass(). The only time super.getClass() == Filter2.class() is true is if this directly extends Filter2. If this extends That extends Filter2, then the comparison will fail, since super.getClass will return That, not Filter2. instanceOf goes through the entire hierarchy.
- 01-23-2009, 07:23 PM #12
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
Thanks for all your replies, they have been helpful. If anyone is interested, here is the code I have come up with which seems to work:
Java Code:public LinkedList<Class> getFilterLevels(){ Class parent = this.getClass(); LinkedList<Class> l = new LinkedList<Class>(); while(!parent.equals(Filter.class)){ l.add(parent); parent = parent.getSuperclass(); } return l; }Last edited by mikeiz404; 01-23-2009 at 09:13 PM. Reason: spelling
Similar Threads
-
using instanceof to get Object type and parent type?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-06-2008, 06:24 PM -
Super class and Subclass in same source file
By makbar24 in forum New To JavaReplies: 17Last Post: 09-10-2008, 01:24 PM -
Class Reflection: Finding super class names
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:12 PM -
Super CSV 1.15
By JavaBean in forum Java SoftwareReplies: 0Last Post: 10-16-2007, 06:37 PM -
Use super. or this.
By Marcus in forum New To JavaReplies: 1Last Post: 07-05-2007, 06:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks