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");}
}
}
Task 2: The “instanceof” operator public class Bird{ public void sing() { System.out.println("Singing!"); } } public cla
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Task 2: The “instanceof” operator public class Bird{ public void sing() { System.out.println("Singing!"); } } public cla
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!