Skip to content

Commit d213237

Browse files
authored
Create Instanceof.java
1 parent 415d16f commit d213237

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Instanceof.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
interface Instanceof{}
2+
class A implements Instanceof
3+
{
4+
public void a()
5+
{
6+
System.out.println("a is method");
7+
}
8+
}
9+
class B implements Instanceof
10+
{
11+
public void b()
12+
{
13+
System.out.println("b is method");
14+
}
15+
}
16+
17+
class Call
18+
{
19+
void invoke(Instanceof p)
20+
{ //upcasting
21+
if(p instanceof A)
22+
{
23+
A a=(A)p; //Downcasting
24+
a.a();
25+
}
26+
if(p instanceof B)
27+
{
28+
B b=(B)p; //Downcasting
29+
b.b();
30+
}
31+
32+
}
33+
} //end of Call class
34+
35+
class Test4{
36+
public static void main(String args[])
37+
{
38+
Instanceof p=new B();
39+
Call c=new Call();
40+
c.invoke(p);
41+
}
42+
}

0 commit comments

Comments
 (0)