Master java skills

instanceof operator

The instanceof operator in java is used to test if an object is an instance of a type (defined by class or a subclass or an interface) or not.

It returns true or false. Let’s see one example

package com.javatrainingschool;

public class Company {

	private String name;

	public static void main(String[] args) {
		
		Company c1 = new Company();
		c1.name = "Java Training School";
		if(c1 instanceof Company)
			System.out.println(c1.name + " is an instance of Company.");
		else
			System.out.println(c1.name + " is not an instance of Company.");
	}
}
Output :

Java Training School is an instance of Company.

Let’s take one more example. Here 2 classes are there. Company and ITCompany. We will create one object of each class and use instanceof operator to test.

package com.javatrainingschool;

public class ITCompany extends Company {

	public static void main(String[] args) {
		
		Company c1 = new ITCompany();
		c1.name = "Java Training School";
		if(c1 instanceof ITCompany)
			System.out.println(c1.name + " is an instance of IT Company.");
		else
			System.out.println(c1.name + " is not an instance of IT Company.");
		
		Company c2 = new Company();
		c2.name = "Tata Motors";
		if(c2 instanceof ITCompany)
			System.out.println(c2.name + " is an instance of IT Company.");
		else
			System.out.println(c2.name + " is not an instance of IT Company.");
	}
}
Output :
Java Training School is an instance of IT Company.
Tata Motors is not an instance of IT Company.

instaceof operator with Interfaces

We can also use instanceof operator to test if object of a class is a particular interface type or not. Let’s see one example

Player is an interface and ChessPlayer is the class that imeplements Player interface.

package com.javatrainingschool;

public interface Player {

	void play();
	
}
package com.javatrainingschool;

public class ChessPlayer implements Player {
	
	
	@Override
	public void play() {
		System.out.println("Player plays chess");
	}

	public static void main(String[] args) {
		
		ChessPlayer chessPlayer = new ChessPlayer();
		boolean result = chessPlayer instanceof Player;
		System.out.println("ChessPlayer is an instance of Player? " + result);
	}
}
Output :

ChessPlayer is an instance of Player? true

Usage of instanceof operator

instanceof operator should be used in casting one object to another. If we directly cast one object to another, and if the objects are incompatible, we would get ClassCastException. Let’s see one example

package com.javatrainingschool;

public class ChessPlayer implements Player {
	
	private String name;
	
	@Override
	public void play() {
		System.out.println("Player plays chess");
	}

	public static void main(String[] args) {
		
		Player player = new ChessPlayer();
		ChessPlayer chessPlayer = null;
		if(player instanceof ChessPlayer) {
			chessPlayer = (ChessPlayer) player;
			chessPlayer.name = "Vishwanathan Anand";
		}
		
		System.out.println(chessPlayer.name);
	}
}
Output :
Vishwanathan Anand