Results 1 to 12 of 12
  1. #1
    mikeiz404 is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Question super instanceof Class?

    Is there any way to determine if the parent class of an object is an instance of another class?

    This seems not to work, but I am unsure why:
    if(super instanceof Object){}
    Last edited by mikeiz404; 01-23-2009 at 03:04 AM. Reason: clarification

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    keyword: instanceof
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    mikeiz404 is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    that doesn't seem to work with the super keyword

  4. #4
    mikeiz404 is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    It kind of begs the question: why do you want to do this? It smells of design problems.

  6. #6
    mikeiz404 is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    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:
    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>();
    		}
    	}
    }
    This way it would be much easier to create a TreeMap of the filters and sort the tree accordingly.
    Is there a better/easier way to do this?
    Last edited by mikeiz404; 01-23-2009 at 05:12 AM. Reason: removed extends FilterHelper

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    =\ no idea what your code does. but you can use the class Class. <= no thats not a typo.
    Java Code:
    if(this.getClass().getSuperclass().getSimpleName().equals("Filter2")) ...
    you can put that in a method to make it easier to read.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Shorter:
    Java Code:
    if(this.getClass().getSuperclass() == Filter2.class) ...
    db

  9. #9
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    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)
    Java Code:
    public String getClassType(final Filter2 pInstance) {
      final String className;
      className = pInstance.getClass.getName();
      return className;
    }
    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.

  10. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default Learned Something New!!

    Quote Originally Posted by Darryl.Burke View Post
    Shorter:
    Java Code:
    if(this.getClass().getSuperclass() == Filter2.class) ...
    db
    where in the jdk doc can I find more info on X.class ? Its not listed under java.lang.Object. what class is this field inherited from?

    Any other .stuff ?? (.length, .class, what else?)
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  11. #11
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    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.

  12. #12
    mikeiz404 is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    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 10:13 PM. Reason: spelling

Similar Threads

  1. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 07:24 PM
  2. Super class and Subclass in same source file
    By makbar24 in forum New To Java
    Replies: 17
    Last Post: 09-10-2008, 02:24 PM
  3. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:12 PM
  4. Super CSV 1.15
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-16-2007, 07:37 PM
  5. Use super. or this.
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 07:52 AM

Posting Permissions

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