Question
For the following code snippet, assume were using a Person class that has a first name, last name and year-of-birth. Conceptually, Person instances should be
For the following code snippet, assume were using a Person class that has a first name, last name and year-of-birth. Conceptually, Person instances should be considered equal if their names and year-of-birth are the same. What is the issue with the following approach weve taken?
[edit -- 3/9/21 9:25am T.Carlson -- added missing 'new' keyword on line for constructing Person 'jonathan']
Person john = new Person("John", "Doe",1945); // ... assume other interesting things happen Person jonathan = new Person("John", "Doe",1945); if( john == jonathan ) { // do something really important here }
Options:
A) We should have used either john or jonathan for both variable names. Using different variables is causing an impedance mismatch
B) We cannot create two instances with the same data like this. Encapsulation & information hiding prevents the sharing of information
C) == for objects is not the proper approach to test equality. We should override and use .equals() rather than == here
D) None of the above
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started