Results 1 to 3 of 3
  1. #1
    ianyappy is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Default Checking class of object

    I've a following piece of code which generates an error for which I don't understand.. Would appreciate some help
    Java Code:
    public class DoggyCheck{
    
    	public static void main(String[] args) {
    		
    		// Spaniel is a subclass of Dog
    		Spaniel pet = new Spaniel ("Duffus");
    		Dog aDogPet;
    
                    // Check if pet is class/subclass of Dog
    		if(pet instanceof Dog)
    			System.out.println("We have a dog!");
    		else
    			System.out.println("It's definitely not a dog!");
    
                    // Check if pet is the exact class Dog (FAIL)
    		if(pet.getClass() == Dog.class)
    			System.out.println("We have a dog!");
    		else
    			System.out.println("It's definitely not a dog!");	
    	}
    }
    Error:
    DoggyCheck.java:12: incomparable types: java.lang.Class<capture#639 of ? extends Spaniel> and java.lang.Class<Dog>
    if(pet.getClass() == Dog.class)

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    The compiler is trying to be smart, i.e. it 'knows' that pet is a Spaniel so its class can never be a Dog.class and it triumphantically starts to whine. Do that test in another method so the compiler loses track of the actual class of the object:

    Java Code:
    class Dog {
    	public Dog(String name) { }
    }
    
    class Spaniel extends Dog {
    	public Spaniel(String name) {
    		super(name);
    	}
    }
    
    public class T {
    	
    	private static void check(Object o) {
    		if(o.getClass() == Dog.class)
    			System.out.println("We have a dog!");
    		else
    			System.out.println("It's definitely not a dog!");	
    	}
    	
    	public static void main(String[] args) {
    		
    		// Spaniel is a subclass of Dog
    		Spaniel pet = new Spaniel ("Duffus");
    		Dog aDogPet;
    
                    // Check if pet is class/subclass of Dog
    		if(pet instanceof Dog)
    			System.out.println("We have a dog!");
    		else
    			System.out.println("It's definitely not a dog!");
    
                    // Check if pet is the exact class Dog (FAIL)
    		
    		check(pet);
    	}
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ianyappy is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  2. Replies: 3
    Last Post: 03-15-2010, 09:44 PM
  3. object vs class
    By billq in forum New To Java
    Replies: 5
    Last Post: 12-24-2009, 05:56 PM
  4. Getting name of object class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 06:22 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 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
  •