Results 1 to 5 of 5
  1. #1
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default confusion about instane of operator

    Hi,
    Hope you all will be fine. In the following code where instance of operator use. Can i call that "a instance of B" means that "a is a super class of B" .

    Java Code:
        class A { }
        class B extends A {
            public static void main (String [] args) {
                A myA = new B();
                m2(myA);
            }
            public static void m2(A a) {
                if (a instanceof B)
                    ((B)a).doBstuff(); // downcasting an A reference to a B reference
            }
            public static void doBstuff() {
                System.out.println("'a' refers to a B");
           }
        }
    I read that if variable on left side of "instance of" passes IS A relationship to the variable on the right then instance of result is true.So if here i say that "a instance of B" means "a IS A B" then it's not true because here "B IS A A" because B extends A.

    Similarly in the code
    Java Code:
        interface Face { }
        class Bar implements Face{ }
        class Foo extends Bar { }
    "Bar instance of Foo" return false why ? above "B extends A" and "a instance of B" return true. Here If we replace on the third line Foo with B and Bar with A then it's the same code as above but above it's return true here false. Can any one clear my confusion about instance of operator.

    Thank you

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

    Default

    Here is what the JLS has to say about it.

    kind regards,

    Jos

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    "instanceof" simply asks if something is an instance of the class on the right.

    In your case, B extends A so an instance of B is an instance of A. In the case of the reference "a", that is a reference to an object of class B. So instanceof returns true.

    In the second example, a reference to an object of class Foo is an instance of Foo, Bar and Face.

  4. #4
    JavaJuJitZu is offline Member
    Join Date
    Jan 2010
    Posts
    26
    Rep Power
    0

    Default

    I think the confusion you have is a class vs object problem.

    the operator doesn't test whether a class is another class, it test whether an object is a instance of a class.

    So saying Bar instance of Foo is only valid when you have a variable name of type Bar named Bar.
    Java Code:
     Bar variableName;
    Here we have declared a variable name of type bar. Currently it holds nothing as is not-instantiated
    We now can instanciate
    Java Code:
     variableName = new Bar()
    we create a new instance of Bar and now try our test
    Java Code:
     variableName instanceof FOO
    this will ofcourse return false as we have assigned a new instance of Bar;
    Java Code:
     variableName = new FOO()
    because foo extends bar we can assign an instance of foo to variable that is of type Bar
    Java Code:
     variableName instanceof FOO
    Here instanceof returns true as the instance assigned to the variable is of type FOO.

    Hope that helps.

  5. #5
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default

    Hi,
    Thanks to all.JavaJuJitZu the way you answered me is very good. Although Tolls said the same thing but you cleared me with a very simple example :).

    Thank you to all :).

Similar Threads

  1. Increment Operator Example
    By abimaran in forum New To Java
    Replies: 10
    Last Post: 11-03-2009, 04:45 PM
  2. Confusing with bitwise NOT operator
    By Willi in forum New To Java
    Replies: 4
    Last Post: 10-16-2009, 11:06 PM
  3. Tic Tac Toe confusion
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 04-12-2009, 01:47 AM
  4. problem with operator in case
    By jimJohnson in forum New To Java
    Replies: 2
    Last Post: 03-21-2008, 08:22 PM
  5. Method/Operator Overloading
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08: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
  •