Java Programming Tutorial Index

Java Fundamentals

The Java instanceof Operator is used to determining whether this object belongs to this particular (class or subclass or interface) or not.



This operator gives the boolean values such as true or false. If it relates to a specific class, then it returns true as output. Otherwise, it returns false as output.

Syntax:

object-reference instanceof type;

Program to Show Downcasting with instanceof Operator

Example:

class Company {}

public class Employee extends Company {
    public void check() {
        System.out.println("Success.");
    }

    public static void view(Company c) {
        if (c instanceof Employee) {
            Employee b1 = (Employee) c;
            b1.check();
        }
    }

    public static void main(String[] args) {
        Company c = new Employee();
        Employee.view(c);
    }
}

Output:

Success.


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram