• Post Reply Bookmark Topic Watch Topic
  • New Topic

duplicate methods ?  RSS feed

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have code as follows :

class Test5 {
public void method(String[] names) {
}
Object method(String... names) {
return null;
}
}

1. Here i have a return type as null whereas I have mentioned a return type as Object. Wouldnt a mismatch occur here ?


2. It gives me an error saying duplicate method. How are these two methods duplicate ? Isn't the return type also a part of the method definition ?

Thanks..
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. No. Null is a reserved constant in java to represent a empty reference (points to nothing).
2. The methods are the same because String[] and String... get compiled to the same. The return type is part of the method definition is this case.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify .. my question was :
1. doesnt the java compiler look for an object return type or any subclass of java.lang.Object ? I just want to understand how the compiler is taking null as a return type when the return is clearly mentioned as Object.

2. The return type is very much a part of the method, so since we have the return types different in both cases, doesnt it actually mean different methods ?

Thanks in advance . Am just trying to get the concepts correct...
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:Just to clarify .. my question was :
You're asking new question now

jose chiramal wrote:1. doesnt the java compiler look for an object return type or any subclass of java.lang.Object ? I just want to understand how the compiler is taking null as a return type when the return is clearly mentioned as Object.
Null is not a return type! You're returning null which is in this case basically an uninitialized Object reference.

jose chiramal wrote:2. The return type is very much a part of the method, so since we have the return types different in both cases, doesnt it actually mean different methods ?
No two methods are the same if they have the same name and paramaters. The return type is not relevant.

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what i can say is that we get a "duplicate method error" when , there are two methods having the same name and same argument(number,order and datatype) , IRRESPECTIVE of their retrun type , CORRECT ??

Thanks
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. The notion of "the same data type" can cause problems; for instance, List<Integer> and List<String> are both the same data type because of type erasure. And your varargs example is another good example where two syntactically different types are still the same data type.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!