-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHasA.java
68 lines (53 loc) · 1.31 KB
/
HasA.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Name {
String name;
Name() {
this.name = "";
}
Name(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public void printName() {
System.out.println("Name: " + this.name);
}
}
class Address {
String address;
Address() {
this.address = "Mountain View, CA";
}
Address(String address) {
this.address = address;
}
public void setAddress(String address) {
this.address = address;
}
public void printAddress() {
System.out.println("Address: " + this.address);
}
}
class Employee {
String company;
Name name;
Address address;
Employee(String name, String company) {
this.name = new Name(name);
this.address = new Address();
this.company = company;
}
public void printCompany() {
System.out.println("Name "+this.name.name+" works at "+this.company);
System.out.println("Address: "+ this.address.address);
}
}
public class HasA {
public static void main(String[] args) {
Employee emp = new Employee("John Doe", "Google");
emp.printCompany();
if (emp.name instanceof Name) {
System.out.println("'emp.name' is an instance of Name");
}
}
}