Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: I have a question related to instanceof operator

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question I have a question related to instanceof operator

    Hi All,

    As per my knowledge, the instanceof operator compiles only if the reference type compared to class type are in the same inheritance tree.
    According to that, below code should not compile but it compiles FINE!!.
    Please correct me if I am wrong and please help me for the reason.

    Output:No output generated since the if condition fails
    public class InstanceOfTest implements Inter{
     
    public static void main(String arg[]){
     
    Inter iot = new InstanceOfTest();
     
    if(iot instanceof Someone) //here Inter(interface) and Someone(class) are not in the same inheritance tree.
    System.out.println("iot is a Someone");
     
    }
     
    }
     
    interface Inter{}
     
    class Someone{}


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I have a question related to instanceof operator

    Cross posted here.

    Thank you for fixing.

    Where do you get the understanding that, "the instanceof operator compiles only if the reference type compared to class type are in the same inheritance tree?"

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Rohan R (February 2nd, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I have a question related to instanceof operator

    Hi Greg,

    I got the below information from SCJP Kathie Sierra:

    You can't use the instanceof operator to test across two different class hierarchies.
    Below code will NOT compile.
    class Cat{}
    class Dog{
     
    public static void main(String arg[]){
     
    Dog d = new Dog();
    System.out.println(d instanceof Cat);
     
    }
    }

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: I have a question related to instanceof operator

    System.out.println(d instanceof Cat);
    That doesn't compile because the println method doesn't accept a boolean.
    Try:
    System.out.println((d instanceof Cat)+"");
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I have a question related to instanceof operator

    Perhaps it would help if you drew out the class hierarchies on paper.

  7. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I have a question related to instanceof operator

    Hi Aussiemcgr,

    System.out.println() CAN take boolean and that is not the issue.

    we can always declare and print boolean values
    boolean b = true;
    System.out.println(b);  //output is true


    --- Update ---

    For my Dog Cat Example, the error is as below:

    C:\java_aug2013>javac Dog.java
    Dog.java:7: error: inconvertible types
    System.out.println(d instanceof Cat);
    ^
    required: Cat
    found: Dog
    1 error


    --- Update ---

    Greg,

    My Examples above are simple and class hierarchies are direct.
    Someone extends Object;
    InstanceOfTest extends Object implements Inter;

    --- Update ---

    Guys,

    Now I can understand that!!!

    1. In Dog and Cat Example, Dog or sub-types of Dog can never be a Cat since Class can extend only one Class.
    Therefore compilation fails.

    2. But in InstanceOfTest Example, there can be a class which is sub-type of Someone and also implements Inter.
    Therefore code need to compile since Compiler never knows what object is being created at runtime

  8. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I have a question related to instanceof operator

    Thank you for explaining.

Similar Threads

  1. Ternary operator
    By JRoberto in forum Other Programming Languages
    Replies: 8
    Last Post: December 29th, 2013, 06:33 PM
  2. Instanceof/cast doesnt work?
    By sci4me in forum Java Theory & Questions
    Replies: 0
    Last Post: May 10th, 2013, 08:53 PM
  3. Abstract classes, and 'instanceOf'
    By jezza10181 in forum Object Oriented Programming
    Replies: 6
    Last Post: August 12th, 2012, 10:45 AM
  4. Just a general Java related question
    By javaisfun in forum Java Theory & Questions
    Replies: 3
    Last Post: October 25th, 2011, 07:21 AM
  5. the meaning of 'instanceof'
    By Abednego Setyawan in forum Object Oriented Programming
    Replies: 1
    Last Post: March 12th, 2010, 01:45 AM

Tags for this Thread