instanceof is a keyword in Java that determines whether an object running is an instance of a particular class, INSTANCEOF returns a Boolean if the object is an instance of a particular class, returns True, and vice versa.
Class Instanceofdemo {public static void main (string[] args) {Cfather C1 = new CChild1 ();//Parent object reference points to subclass Object Test (C1); Cfather C2 = new CChild2 (); test (C2);} public static void Test (Cfather c) {c.print (); Use instanceof to determine if C is an instance of CChild2 if (c instanceof CChild2) {CChild2 C2 = (CChild2) c;c2.print1 ();}}} Abstract class Cfather{abstract public void print ();//Abstraction Method}class CChild1 extends cfather{//public void print () { System.out.println ("This is Child1");}} Class CChild2 extends cfather{public void print () {System.out.println ("This is Child2");} Other methods of this class public void Print1 () {System.out.println ("Hello java");}}
The instanceof keyword in Java