• Post Reply Bookmark Topic Watch Topic
  • New Topic

Two Classes with Identical Constructors?  RSS feed

 
Anakela Bella
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question! Is it possible to combine two classes that I have defined to contain some of the same elements so that NetBeans stops giving me errors? I don't want to get rid of any necessary code, and if both classes are necessary, should I just rename one of them? One class is an ArrayList that I am using to write the information for employees entered to a text file "employee.txt." I also want users to be able to call on this information via employeeID in order to display employee information. The code is the following:


The other class is a getEmployee class that I previously defined before attempting to read the information from the text file and display it in the console. It is as follows:
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NetBeans doesn't give any errors. It simply passes on the errors which the javac tool has found.

Don't return null from anywhere if you can help it. Consider changing that method so you pass a List as an argument and the method populates the List. Then you can query the List later and see whether it is empty. Or even better, allow the Exception to propagate and catch it farther up the call stack.
You cannot have two classes with identical constructors unless they have the same simple name. That means they have to be in different packages, in which case yes they can have identical constructors. But I don't think that is what you really want to know. I think when you have such a complicated question that there might be something fundamentally wrong with your design and you have the wrong classes somewhere. But you haven't given enough information for us to know that. What classes have you got and what is their inheritance hierarchy?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You talk about classes and constructors, but you probably mean methods.

You cannot have two methods in one class that have the same name and the same argument list - so, for example, you cannot have two methods named getEmployee which both take no parameters. That's because there's no way to tell the two methods apart - when you call getEmployees(), how is the Java compiler supposed to know which of the two methods you mean?

you should rename one of them. Rename the second one, that reads from a file, to readEmployees(), for example.
 
Anakela Bella
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:NetBeans doesn't give any errors. It simply passes on the errors which the javac tool has found.

Don't return null from anywhere if you can help it. Consider changing that method so you pass a List as an argument and the method populates the List. Then you can query the List later and see whether it is empty. Or even better, allow the Exception to propagate and catch it farther up the call stack.
You cannot have two classes with identical constructors unless they have the same simple name. That means they have to be in different packages, in which case yes they can have identical constructors. But I don't think that is what you really want to know. I think when you have such a complicated question that there might be something fundamentally wrong with your design and you have the wrong classes somewhere. But you haven't given enough information for us to know that. What classes have you got and what is their inheritance hierarchy?


Thanks for your help, Campbell Ritchie. I have lots of different classes, so I'm not sure if the following will help. However, the classes in this application (and their hierarchies), are as follows:







 
Anakela Bella
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:You talk about classes and constructors, but you probably mean methods.

You cannot have two methods in one class that have the same name and the same argument list - so, for example, you cannot have two methods named getEmployee which both take no parameters. That's because there's no way to tell the two methods apart - when you call getEmployees(), how is the Java compiler supposed to know which of the two methods you mean?

You should rename one of them. Rename the second one, that reads from a file, to readEmployees(), for example.


Yes, I do mean methods! Sorry about that! Hehe... I may try to rename the second method. I was thinking of just getting rid of the method that is not the arraylist, but I'm afraid that if I get rid of it, I will end up being unable to add new items to the arraylist and text file. As I may have stated previously, I'm super new to Java, and have A LOT to learn.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!