-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInstance.java
More file actions
48 lines (39 loc) · 1008 Bytes
/
Instance.java
File metadata and controls
48 lines (39 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import packageName.Parent;
// Java program to demonstrate working of instanceof Keyword
// Class 1
// Parent class
// Class 2
// Child class
class Child extends Parent {
}
// Class 3
// Main class
class Instance extends Child {
// Main driver method
public static void main(String[] args)
{
// Creating object of child class
Child cobj = new Child();
// A simple case
if (cobj instanceof Child)
System.out.println("cobj is instance of Child");
else
System.out.println(
"cobj is NOT instance of Child");
// instanceof returning true for Parent class also
if (cobj instanceof Parent)
System.out.println(
"cobj is instance of Parent");
else
System.out.println(
"cobj is NOT instance of Parent");
// instanceof returns true for all ancestors
// Note : Object is ancestor of all classes in Java
if (cobj instanceof Object)
System.out.println(
"cobj is instance of Object");
else
System.out.println(
"cobj is NOT instance of Object");
}
}