This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
Enthuware Software Support
Joined: Sep 23, 2000
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
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