Solved

java constructor error

Posted on 2016-08-19
8
88 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Scripting vs. Programming languages 25 117
Java and GPO 11 47
thymeleaf natural templating vs JSP 2 30
Java Jpanels and Jframe 8 20
This is an explanation of a simple data model to help parse a JSON feed
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now