Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Object resolution

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {}
class B extends A {}
class C extends B {}
class D extends C {}
The question is
Does (obj instanceof B) && ! (obj instanceof C)
correctly identifies whether the object referred to by obj was created by instantiating class B rather than classes A, C and D?
The answer is true. I agree that !(obj instanceof C) says that it does not belong to C or it's subclass D
But, how does it say that it does not belong to superclass A as
A is not mentioned in (obj instanceof B) && ! (obj instanceof C)
if the answer was
(obj instanceof B) && ! (obj instanceof C) && ! (obj instanceof A), then it is perfectly clear.
Any help is greatly appreciated.
-Jay
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you had created the object like this
A a = new A();
then the expression would evaluate to false, since A is not a subclass of B, but with the current expression, AS IS, the answer is correct since it correctly identifies an instance of class B and not A because obj is not an instance of C but is an instance of B!
Val
 
You got style baby! More than this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic