Solved

java constructor error

Posted on 2016-08-19
8
117 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
8 Comments
 
LVL 10

Accepted Solution

by:
Jeffrey Dake earned 251 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 83 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 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 83 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 26

Assisted Solution

by:dpearson
dpearson earned 83 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTPSessionBindingListernter not configured in deployment descriptor 2 52
VbScript to countdown to New Year's Day 6 54
Cygwin - GNU GPL License 1 23
ejb on wildfly 5 19
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

805 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