Skip to content

Commit 7039c8c

Browse files
authored
Create InstanceOfKeyword.java
1 parent 7bac0dc commit 7039c8c

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Author: Atharv Damle
2+
// Code to demonstrate use of instanceof keyword.
3+
// Full Question: https://www.hackerrank.com/challenges/java-instanceof-keyword/problem
4+
5+
// a instanceof b checks if 'a' is a subclass of 'b'. Returns true if yes otherwise false.
6+
// Since the instance of any class is inherited from Java Object, element acts as a sort of generic data type
7+
// which can be instanciated by any class.
8+
Object element=mylist.get(i);
9+
if(element instanceof Student)
10+
a++;
11+
else if(element instanceof Rockstar)
12+
b++;
13+
else if(element instanceof Hacker)
14+
c++;

0 commit comments

Comments
 (0)