• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

InstanceOf operator

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am just confused about instance of operator,i just want to know whether instance of testes about object type or class type
for example
if class A extends class B and we create a object like this

A a=new B();
if we test
if(a instanceof b) it returns true and if we test
if(a instanceof A) then also it returns true
please reply


 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suresh KumarPandey wrote:
A a=new B();
if we test
if(a instanceof b) it returns true and if we test
if(a instanceof A) then also it returns true
please reply



And you are confused because .... ??? In both cases, it returns true because it is true. You need to, at least, tell us why you think it should not be -- as we don't know where you are confused.

Henry
 
Enthuware Software Support
Posts: 4650
51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The name of the operator is very descriptive. It does exactly what it says i.e. "Is this object an instance of this type?" It checks where the given object is of some type. Now, type is not necessarily a specific class. I can also be a superclass. For example, you know that a particular instance of a car is a Car. So if car is a variable pointing to an instance of class Car, car instanceof Car will be true. You already know that.

But can you say that a car is an Automobile? If you can say yes, then car instanceof Automobile should also return true.

Can you say that a car is a Fruit? Well, then you know what car instanceof Fruit would return

Apply the same logic to your A and B and you will understand how it works.

HTH,
Paul.
 
Paul Anilprem
Enthuware Software Support
Posts: 4650
51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suresh KumarPandey wrote:i am just confused about instance of operator,i just want to know whether instance of testes about object type or class type
for example
if class A extends class B and we create a object like this

A a=new B();


BTW, if A extends B then this line of code will not compile. May be you mean, B extends A and probably that is your cause of confusion
 
Suresh KumarPandey
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Paul Anilprem thanks for the reply ,i got it
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Day,

A very interesting perspective about the instanceof operator at Beware of instanceof operator.

Regards,
Dan
 
I was her plaything! And so was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic