Page 1 of 1

Task 2: The “instanceof” operator public class Bird{ public void sing() { System.out.println("Singing!"); } } public cla

Posted: Wed Apr 27, 2022 3:30 pm
by answerhappygod
Task 2: The “instanceof” operator
public class Bird{
public void sing()
{
System.out.println("Singing!");
}
}
public class Eagle extends Bird{ }
public class Test
{
public static void main(String[] args)
{
Bird e1 = new Eagle();
Eagle e2 = new Eagle();
Bird b1 = new Bird();
System.out.println(e1 instanceof Bird);
System.out.println(e2 instanceof Bird);
System.out.println(e2 instanceof Eagle);
if (b1 instanceof Eagle )
{
Eagle e3 =
(Eagle) b1;
System.out.print(e3);
} else {System.out.println("TypeCasting
cant be done");}
}
}