0

The output of the below snippet is 012, but kindly let me know how (b2 instanceof Toy) is getting true.

kindly explain on this.

interface Vessel { }  
interface Toy { }  
class Boat implements Vessel { }  
class Speedboat extends Boat implements Toy { }  
public class Tree {  
    public static void main(String[] args) 
    {  
         String s = "0";  
         Boat b = new Boat();  
         Boat b2 = new Speedboat();  
         Speedboat s2 = new Speedboat();  
         if((b instanceof Vessel) && (b2 instanceof Toy)) s += "1";  
         if((s2 instanceof Vessel) && (s2 instanceof Toy)) s += "2";  
         System.out.println(s);  
     }  
}  
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
Raphael
  • 1,738
  • 2
  • 27
  • 47

7 Answers7

1

You can assign an object to a variable of any type in its hierarchy, but instanceof will always examine the object itself when being evaluated. b2 references a Speedboat object, which implements Toy, hence b2 instanceof Toy is true.

Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
0

I believe this post will help you understand clearly.

Taken from What is the 'instanceof' operator used for?

instanceof keyword is a binary operator used to test if an object (instance) is a subtype of a given Type.

As the post explains, since Toy is implemented by SpeedBoat, the instanceof operator returns true for that condition.

Also the official oracle documentation of tutorial explains instanceof as:

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

Community
  • 1
  • 1
anirudh
  • 4,116
  • 2
  • 20
  • 35
0

instanceof tests if the reference points to an object whose concrete type is, extends, or implements (directly or indirectly) the given type.

The object referenced by b2 is of type SpeedBoat. SpeedBoat implements Toy. So the object referenced by b2 is a Toy (i.e. is an instance of Toy).

instanceof is a synonym for "is a" in real life. If a child shows you a speed boat and asks "is that a toy", you'll answer yes. If he asks "is it a speed boat", you'll answer yes. If he asks "is it an object", you'll answer yes. If he asks "is it a banana", you'll answer no.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

b2 is an instance of Speedboat, a class that implements Toy. As Speedboat extends Boat, it can be assigned to a type higher in the type hierarchy to it (Boat b2 = new Speedboat();), but as it is still an instance of Speedboat (try b2.getClass()), b2 instanceof Toy will still return true.

s.d
  • 4,017
  • 5
  • 35
  • 65
0

For example by the line

   s2 instanceof Vessel 

it is trying to find whether s2 is in the same hierarchy down the line of Vessel. Not necessarily direct relation but next to next or more distant relation would be answered true, if the 2 comparing class exist in same hierarchy. But instanceof will return false if they have no direct hierarchy. Like

    interface A{..}

    interface B{..}

    class C implements A {..}

then

    C instanceof B 

will return false and

    C instanceof A 

will return true

anirban
  • 674
  • 1
  • 7
  • 21
0

Be aware that instanceof should be used sparingly. If you find yourself writing code that uses instanceof, then investigate shifting the target code to a new member method in each subclass (which is usually where it belongs). In your code, both Boat and Speedboat could provide a method int getScore() that would remove the need for instanceOf.

One place that it is invaluable is in exception handling. For instance:

try {
  .. Access the database
} catch (Exception ex) {
  if (ex instanceof SQLException) {
    .. Process the details of the database exception to log more detail
  } else {
    throw ex;

}

kiwiron
  • 1,677
  • 11
  • 17
  • Err, no. instanceof should not be used in exception handling either. Just catch SQLException instead of or before catching Exception if you want to do something special if the exception is a SQLException. – JB Nizet May 08 '14 at 08:18
  • Thanks JB Nizet - you are quite right. Which leaves me wondering when instaceof is appropriate - if ever? – kiwiron May 08 '14 at 08:37
  • Every time polymorphism is really not an option. For example, let's say you want a method that accepts an array of objects (Date, String, Enum, Integer, etc.), and each object must be inserted into a database column. You'll use instanceof to know the type of the object and call setInt(), setString() or setDate() on the prepared statement. – JB Nizet May 08 '14 at 08:42
-2

(a instanceOf b) means the instanceOf operator returns true only if a belongs to the type of b.. Ex-If Dog is a super class and puppy is a sub class.then (Dog instanceOf puppy returns) true.

In your example its Vessel-->Boat-->speedBoat