-
Notifications
You must be signed in to change notification settings - Fork 1
/
InstanceOf.java
48 lines (37 loc) · 1008 Bytes
/
InstanceOf.java
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
interface Human {}
class Person implements Human {
String name;
Person(String name) {
this.name = name;
}
void printName() {
System.out.println("Name: " + name);
}
}
class Employee extends Person {
String company;
Employee(String name, String company) {
super(name);
this.company = company;
}
void printCompany() {
System.out.println("Company: " + company);
}
}
public class InstanceOf {
public static void main(String[] args) {
Person p = new Person("Anurag");
Employee e = new Employee("Anurag Peddi", "Microsoft");
e.printName();
e.printCompany();
if (e instanceof Employee) {
System.out.println("e is an instance of Employee");
}
if (p instanceof Person) {
System.out.println("p is an instance of Person");
}
if (p instanceof Human) {
System.out.println("p is an instance of Human");
}
}
}