Analyze the following code.
```
// Program 1:
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}
// Program 2:
public class Test {
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}
```
a. Program 1 displays true and Program 2 displays true
b. Program 1 displays false and Program 2 displays true
c. Program 1 displays true and Program 2 displays false
d. Program 1 displays false and Program 2 displays false
b. Program 1 displays false and Program 2 displays true
In Program 1, the equals method in the Object class is invoked. In Program 2, the equals method in the class A is invoked. There are now two overloaded methods available in the class A. i.e. public boolean equals(Object a) and public boolean equals(A a). Which of the two is used by a1.equals(a2) is determined at compile time. a1.equals(a2) in Program 1 matches the equals method defined in Object and a1.equals(a2) in Program 2 matches the equals method defined in the class A.
You might also like to view...
In an e-mail header, which box would include a brief synopsis of the message?
A) subject B) closing C) attachments D) salutation
Which of the following is NOT a type of alternative search engine?
A) Blog search engine B) Keyword search engine C) Job search engine D) Human search engine