Solved

java constructor error

Posted on 2016-08-19
8
132 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 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 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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 27

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 40
Eclipse Java import and method not resolved 4 73
collection output issue 9 59
Problem to copy file 14 45
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

740 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