Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

java constructor error

Hi,

I created below Person class with 3 constructors. One with all the fields second one with top 3 fields and third one with bottom three fields.

I am getting below error. can you please advise how to resolve it.

"Duplicate method Person(String, String, String) in type Person"
person.png
ASKER CERTIFIED SOLUTION
Avatar of Jeffrey Dake
Jeffrey Dake
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

how do i fulfill this requirement of having 2 constructors with 3 strings as argument where each string is different but compiler complaining?
Looking at your scenario I would solve it by not storing date as a String.  I would use a date object, which would then change your signature and make it work.
Avatar of gudii9

ASKER

got it. it looks strange if i have got 6 different kinds of string fields, i cannot create one constructor with top 3 fields other second constructor with bottom 3 fields?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

Another way to solve this is to switch from a constructor to a factory method - which means a static method which constructs the object for you.

factory method internally calling generica 6 argument costructor right with rest of 3 arguments as null?
public Person(String name, String sex, String joinDate, String ss_number,...) {
}

is it same as factory design pattern approach?