?
Solved

java constructor error

Posted on 2016-08-19
8
Medium Priority
?
146 Views
Last Modified: 2016-08-21
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
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 10

Accepted Solution

by:
Jeffrey Dake earned 1004 total points
ID: 41763362
The compiler does not care what you name your variables.  What it sees is two constructors that have 3 string arguments.   If you were two call the constructor passing in three Strings like

Person personObj = new Person("one","two","three");

Java wouldn't be able to figure out if you meant the constructor that starts with name or the one with address. Therefore the compiler does not allow two constructors with the same signature.
0
 
LVL 1

Assisted Solution

by:sunilgohel
sunilgohel earned 332 total points
ID: 41763392
In OOPs we cannot have same function with same signature. Operator overloading allows us same name function but the signature must be different.


Person(string param1,string param2,string param3){}

Person(string param1,string param2){}

This is allowed. but below is not allowed.

Person(string param1,string param2,string param3){}

Person(string param4,string param5,string param6){}
0
 
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 332 total points
ID: 41763675
You have to read the error message - in the type Person, you have 2 definitions of the person method. You are only allowed one definition of a method within a type.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Author Comment

by:gudii9
ID: 41763680
how do i fulfill this requirement of having 2 constructors with 3 strings as argument where each string is different but compiler complaining?
0
 
LVL 10

Expert Comment

by:Jeffrey Dake
ID: 41763696
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41763709
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?
0
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 332 total points
ID: 41763787
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.

In code:

public Person(String name, String sex, String joinDate, String ss_number,...) {
}

public static Person withJoinDate(String name, String sex, String joinDate) {
   return new Person(name, sex, joinDate, null, null, null) ;
}

public static Person withShift(String address, String job, String shift) {
   return new Person(null, null, null, address, job, shift) ;
}

Open in new window


So now to construct somebody with a join date you call:
Person newPerson = Person.withJoinDate(name, sex, joinDate) ;

and there's no collision with
Person otherPerson = Person.withShift(address, job, shift) ;

even though both take 3 strings, since the static method names are different.

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 41764417
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?
0

Featured Post

Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question