Results 1 to 5 of 5
- 02-03-2010, 05:36 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 48
- Rep Power
- 0
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" .
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.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"); } }
Similarly in the code
"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.Java Code:interface Face { } class Bar implements Face{ } class Foo extends Bar { }
Thank you
- 02-03-2010, 05:59 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Here is what the JLS has to say about it.
kind regards,
Jos
- 02-04-2010, 09:21 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
"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.
- 02-04-2010, 10:52 AM #4
Member
- Join Date
- Jan 2010
- Posts
- 26
- Rep Power
- 0
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.
Here we have declared a variable name of type bar. Currently it holds nothing as is not-instantiatedJava Code:Bar variableName;
We now can instanciate
we create a new instance of Bar and now try our testJava Code:variableName = new Bar()
this will ofcourse return false as we have assigned a new instance of Bar;Java Code:variableName instanceof FOO
because foo extends bar we can assign an instance of foo to variable that is of type BarJava Code:variableName = new FOO()
Here instanceof returns true as the instance assigned to the variable is of type FOO.Java Code:variableName instanceof FOO
Hope that helps.
- 02-08-2010, 06:28 AM #5
Member
- Join Date
- Aug 2009
- Posts
- 48
- Rep Power
- 0
Similar Threads
-
Increment Operator Example
By abimaran in forum New To JavaReplies: 10Last Post: 11-03-2009, 04:45 PM -
Confusing with bitwise NOT operator
By Willi in forum New To JavaReplies: 4Last Post: 10-16-2009, 11:06 PM -
Tic Tac Toe confusion
By jigglywiggly in forum New To JavaReplies: 15Last Post: 04-12-2009, 01:47 AM -
problem with operator in case
By jimJohnson in forum New To JavaReplies: 2Last Post: 03-21-2008, 08:22 PM -
Method/Operator Overloading
By Java Tip in forum Java TipReplies: 0Last Post: 12-01-2007, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks