Java. The instanceof operator. Determining the type of object. Examples

The instanceof operator. Determining the type of object. Examples


Contents


Search other websites:




1. The purpose of the instanceof operator. General form

The instanceof operator is used to determine the type of the object at runtime. Having determined the type of the object, you can implement the correct typecasting in the case when the classes form a hierarchy. If you perform an incorrect cast to a certain type, then an exception will be generated at the runtime, which is unacceptable.

The general form of the instanceof operator is as follows

reference instanceof type

here

  • reference – reference to the object (instance) of class;
  • type – class type.

The result of the instanceof operator is the value of true or false. The result true is returned in the following cases:

  • if reference is of the specified type;
  • if reference can be cast to the specified type.

Otherwise, false is returned.

 

2. Using instanceof for classes that form a hierarchy. Example

The example demonstrates the correct definition of the type to which the class object belongs. Implemented two classes A and B, which form a hierarchy. For demonstration purposes, the type of the corresponding object is defined, taking into account that the inherited class extends the capabilities of the superclass.

// The instanceof operator
// Superclass A
class A {
  int a;
}

// Class B - inherits from class A
class B extends A {
  int b;
}

public class TestInstanceOf {

  public static void main(String[] args) {
    A objA = new A();
    B objB = new B();

    if (objA instanceof A)
      System.out.println("objA is a instance of A");
    else
      System.out.println("objA is not an instance of A");

    if (objA instanceof B)
      System.out.println("objA can be cast to type B");
    else
      System.out.println("objA can not be cast to type B");

    if (objB instanceof A)
      System.out.println("objB can be cast to type A");
    else
      System.out.println("objB can not be cast to type A");

    if (objB instanceof B)
      System.out.println("objB is a instance of B");
    else
      System.out.println("objB is not an instance of B");
  }
}

The result of the program

objA is a instance of A
objA can not be cast to type B
objB can be cast to type A
objB is a instance of B

As you can see from the result, an instance of the inherited class B can be correctly cast to the superclass A. But the instance of the superclass A cannot be extended to the capabilities of the inherited class B.

 

3. Using instanceof on type Object. Example

In the Java Object Model, all classes you create inherit from the Object class. The following example accounts for this statement.

// The instanceof operator
// Superclass A
class A {
  int a;
}

// Subclass
class B extends A {
  int b;
}

public class TestInstanceOf {

  public static void main(String[] args) {
    A objA = new A(); // instance of class A
    B objB = new B(); // instance of class B

    // Checking for inheritance from the Object class
    if (objA instanceof Object)
      System.out.println("objA can be cast to type Object");
    else
      System.out.println("objA can not be cast to type Object");

    if (objB instanceof Object)
      System.out.println("objB can be cast to type Object");
    else
      System.out.println("objB can not be cast to type Object");
  }
}

The result of the program

objA can be cast to type Object
objB can be cast to type Object

 

4. Using instanceof to reference the base class. Example

If the classes form an inheritance hierarchy, then a superclass reference can get the value of instances of any derived class. The example below demonstrates whether the reference to superclass can be cast to the type of derived class.

// The instanceof statement
// Superclass A
class A {
  int a;
}

// Subclass B
class B extends A {
  int b;
}

// Subclass C
class C extends B {
  int c;
}

// Subclass D
class D extends C {
  int d;
}

public class TestInstanceOf {

  public static void main(String[] args) {
    // Instances of classes A, B, C, D
    A objA = new A();
    B objB = new B();
    C objC = new C();
    D objD = new D();

    // A reference to the superclass
    A refA;

    // A superclass reference can be assigned the values
    // of instances of derived classes
    refA = objA;

    if (refA instanceof A)
      System.out.println("refA is an instance of A");
    else
      System.out.println("refA is not an instance of A");

    // Redirect reference to the objB instance
    refA = objB;
    if (refA instanceof B)
      System.out.println("refA can be cast to type B");
    else
      System.out.println("refA can not be cast to type B");

    if (refA instanceof D)
      System.out.println("refA can be cast to type D");
    else
      System.out.println("refA can not be cast to type D");

    // Reference to objC
    refA = objC;
    if (refA instanceof B)
      System.out.println("refA can be cast to type B");
    else
      System.out.println("refA can not be cast to type B");
  }
}

The result of the program

refA is an instance of A
refA can be cast to type B
refA can not be cast to type D
refA can be cast to type B

 


Related topics