• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method already defined problem  RSS feed

 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all.

I'm building a Java webapp and I'm also creating a Session API is handle all my session stuff.

I have one problem..

I've got these 2 methods defined and compiled:



When I try and add this..



I get the following compilation error:



It's lets me define get() with two different data types but when I add a third different data type (ArrayList()) it says get() is already defined. Any ideas? Help much appreciated!
 
Pete Letkeman
Ranch Foreman
Posts: 900
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList has a built in method named get() and I suspect there is a naming collision.
I suspect that if you were to rename this to something like getSessionID() then things would work fine.
Or possibly your package name is missing or not defined correctly? 
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Letkeman wrote:ArrayList has a built in method named get() and I suspect there is a naming collision.
I suspect that if you were to rename this to something like getSessionID() then things would work fine.
Or possibly your package name is missing or not defined correctly? 


My package name is:



I have a setter method for it as well (which compiles, as I've got 3 setter methods with the same name (set()) just different data types:



What has this got to do with the session id? Dont mean to be rude lol
 
Pete Letkeman
Ranch Foreman
Posts: 900
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right now, I'm just grasping as straws, hoping to help you out as I'm somewhat new to Java myself.
I suspect, but I could be 100% wrong, that for whatever reason the Java is being tripped up and it thinks that you are trying to override a built in method of ArrayList.

I understand that you wish to use a method name get. However does this work when you change the name of the method?
Michael Portman wrote:What has this got to do with the session id? Dont mean to be rude lol

Nope, you are 100% fine. I just threw out session id as example.
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Letkeman wrote:Right now, I'm just grasping as straws, hoping to help you out as I'm somewhat new to Java myself.
I suspect, but I could be 100% wrong, that for whatever reason the Java is being tripped up and it thinks that you are trying to override a built in method of ArrayList.

I understand that you wish to use a method name get. However does this work when you change the name of the method?
Michael Portman wrote:What has this got to do with the session id? Dont mean to be rude lol

Nope, you are 100% fine. I just threw out session id as example.


Dont forget HashHap has got a get() method too and that works..
 
Pete Letkeman
Ranch Foreman
Posts: 900
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Portman wrote:Dont forget HashHap has got a get() method too and that works..

Yes, I do understand this and I'm not disagreeing with you in any way.

In the even that you change the name of the method from get to something else does your code compile?
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Letkeman wrote:
Michael Portman wrote:Dont forget HashHap has got a get() method too and that works..

Yes, I do understand this and I'm not disagreeing with you in any way.

In the even that you change the name of the method from get to something else does your code compile?


It will work if I do that but I shouldn't have to if you know what I mean?
 
Pete Letkeman
Ranch Foreman
Posts: 900
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I think I see what may be the issue:

You have these methods in the same class:
public Map<String, String> get()
public ArrayList<String> get()

You cannot do that. This is illegal, because the names are the same.

When you are overriding methods you need to keep the same parameter/argument list and return a covarient type.
When you are overloading you need change the parameter/argument list.

So you are trying to override the get method, which you have already defined.
 
Pete Letkeman
Ranch Foreman
Posts: 900
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we take out the extra objects then we can bring this back to the following:

Either on either line 2 you would need to change the name or on line 3 you would need to change the name.
Line 4 works fine, this is a valid overload as the parameter/arg list has changed.

Edit: Invalid comments in code fixed
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Letkeman wrote:Oh, I think I see what may be the issue:

You have these methods in the same class:
public Map<String, String> get()
public ArrayList<String> get()

You cannot do that. This is illegal, because the names are the same.

When you are overriding methods you need to keep the same parameter/argument list and return a covarient type.
When you are overloading you need change the parameter/argument list.

So you are trying to override the get method, which you have already defined.


So to summarize I can only have:



and not:



Is there anyway around this as I want them to have the same method name, it just looks cleaner..
 
Pete Letkeman
Ranch Foreman
Posts: 900
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Portman wrote:Is there anyway around this as I want them to have the same method name, it just looks cleaner.

From what I know, no there is no way to do exactly what you want.
There may be some way that you could do this with inner classes or inner interfaces, of which I do not know too much about.

However now that you know the exact cause of the problem it may be worth it to start a new thread so that some other people can chime in with advice/help.
There are some people on this site who are really well versed in Java.
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method signature is based on the method's name and the parameter types only - the method's return type is not part of the signature.

In your class
    public Map<String, String> get()
    public ArrayList<String> get()
have the same signature, which is why you are getting a error message stating that method get() is already defined in class Session.

You could fix the problem by giving one of the methods a different name (maybe something like getList() for the method which returns an List).
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Letkeman wrote:
Michael Portman wrote:Is there anyway around this as I want them to have the same method name, it just looks cleaner.

From what I know, no there is no way to do exactly what you want.
There may be some way that you could do this with inner classes or inner interfaces, of which I do not know too much about.

However now that you know the exact cause of the problem it may be worth it to start a new thread so that some other people can chime in with advice/help.
There are some people on this site who are really well versed in Java.


Im mainly a PHP developer by trade. I've known and practiced PHP since PHP4 and I have many projects. I want to learn Java to the same level of knowledge of my PHP experience. The main reason for this is because where I live (the UK) there is a shortage of Java Web Developers and Java Web Developers get paid more than PHP Developers. Dunno about you tho..


 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:The method signature is based on the method's name and the parameter types only - the method's return type is not part of the signature.

In your class
    public Map<String, String> get()
    public ArrayList<String> get()
have the same signature, which is why you are getting a error message stating that method get() is already defined in class Session.

You could fix the problem by giving one of the methods a different name (maybe something like getList() for the method which returns an List).


Ill have to do that then. As I'm forced to . Oracle should allow us to this.
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When renaming, maybe try and make the method names more specific as well so that they describe what you are returning.  You may find later, that you may want to get other information as well.  For example, if the get methods are returning attributes, you could name the methods something like:
    String getAttributeValue(String name)
    List<String> getAttributeNames()
    Map<String, String> getAttributeMap()
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Portman wrote:Oracle should allow us to this.

I don't think it should.

Consider this example. It is the same case you tried (two methods with the same name and parameters but with different return type).

You want this to be possible:Let's create an implementation:And now it's time to use it.In your opinion, what should this program print?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Portman wrote:. . . Oracle should allow us to this.
Apart from the fact that Paweł has already shown why this shou‍ld not be permitted, don't blame Oracle. That definition of method signatures goes back long before Oracle were involved with Java®, probably back to the days of JDK1.0.

The Java® Language Specification calls two methods with the same signature override‑equivalent (two hits in the index: 1 2) and there are restrictions on two override‑equivalent methods being permitted to compile.
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK it doesn't matter, we don't need to make an issue of it. I can simply rename the method. Job done. Issue closed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!