Instanceof Keyword in Java

Here I have attached the Simple example for instanceof in Java programming language

package com.javatraineronline;

public class Employee {

	public void work() {
		
		System.out.println("Employee is Working");
	}
}
package com.javatraineronline;

public class Engineer extends Employee {

	@Override
	public void work() {

		System.out.println("Engineer is Working");
	}
}
package com.javatraineronline;

public class Manager extends Employee {

	@Override
	public void work() {

		System.out.println("Manager is Working");
	}
}
package com.javatraineronline;

public class Tax {

	public double calculateTax(Employee employee) {
		
		double taxPercentage = 0.0; // Default percentage
		
		if(employee instanceof Manager) { // if the 'employee' instance of Manager
			taxPercentage = 12.5;
		} else if(employee instanceof Engineer) { // if the 'employee' instance of Engineer
			taxPercentage = 10.2;
		}
		
		return taxPercentage;
	}
}
package com.javatraineronline;

public class EmployeeApplication {

	public static void main(String[] args) {
		
		// Creating instance of Tax
		Tax incomeTax = new Tax();
		double taxPercentage; // Variable to store the tax percentage.
		
		Employee employee = null;
		
		employee = new Employee();
		employee.work();
		taxPercentage= incomeTax.calculateTax(employee);
		System.out.println("Income Tax : "+taxPercentage+"%"); // Employee Tax
		
		System.out.println(); // For new line
		employee = new Manager(); // 'employee' referring Manager Object
		employee.work();
		taxPercentage = incomeTax.calculateTax(employee);
		System.out.println("Income Tax : "+taxPercentage+"%"); // Manager Tax
		
		System.out.println(); // For new line
		employee = new Engineer(); // 'employee' referring Engineer Object
		employee.work();
		taxPercentage = incomeTax.calculateTax(employee);
		System.out.println("Income Tax : "+taxPercentage+"%"); // Engineer Tax
		
	}
}

[su_box title=”Output for EmployeeApplication.java”]

Employee is Working
Income Tax : 0.0%

Manager is Working
Income Tax : 12.5%

Engineer is Working
Income Tax : 10.2%

[/su_box]