Skip to content

Commit cfc946b

Browse files
use of instanceof operator
1 parent c359f04 commit cfc946b

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

InstrumentMain.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
abstract class Instrument
2+
{
3+
public abstract void Play();
4+
}
5+
class Piano extends Instrument
6+
{
7+
public void Play()
8+
{
9+
System.out.println("Piano is playing tan tan tan tan");
10+
}
11+
}
12+
13+
class Flute extends Instrument
14+
{
15+
public void Play()
16+
{
17+
System.out.println("Flute is playing toot toot toot toot ");
18+
}
19+
}
20+
21+
class Guitar extends Instrument
22+
{
23+
public void Play()
24+
{
25+
System.out.println("Guitar is playing tin tin tin");
26+
}
27+
}
28+
29+
class InstrumentMain {
30+
31+
public static void main(String args[]) {
32+
Instrument A[] = new Instrument[10];
33+
for (int i = 0; i< 10; i++) {
34+
switch (i % 3)
35+
{
36+
case 0: {
37+
A[i] = new Piano();
38+
break;
39+
}
40+
case 1: {
41+
A[i] = new Flute();
42+
break;
43+
}
44+
case 2: {
45+
A[i] = new Guitar();
46+
break;
47+
}
48+
}
49+
}
50+
for (int i = 0; i < 10; i++) {
51+
System.out.println((i + 1));
52+
A[i].Play();
53+
if (A[i] instanceof Piano) {
54+
System.out.println("Piano");
55+
}
56+
if (A[i] instanceof Flute) {
57+
System.out.println("Flute");
58+
}
59+
if (A[i] instanceof Guitar) {
60+
System.out.println("Guitar");
61+
}
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)