• Post Reply Bookmark Topic Watch Topic
  • New Topic

function overloading in java  RSS feed

 
sinatra roger
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi people,

i am posting this to ensure if a paragraph i read in "thinking in java" is understood by me correctly.

so here's the paragraph:

name hiding

only c++ programmers might be surprised by name hiding,since it works differently in that language, If a java base class has a method name thats overloaded several times,redefining that method name in the derived class will not hide any of the base class any of the base class versions. Thus overloading works whether the method was defined at this level or in a base class




the above c++ code creates an error when we call fun by passing 10 to it as fun expects an array of chars.

now heres the java code:


here since there is no appropriate signature in the derived class, the matching method in base class is called..Is this what the author says..please correct me if i am wrong. also i dont clearly understand the term 'name hiding'
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, your thinking is correct.

Name hiding refers to that you can't use a name to refer to one thing, as it's already being used to refer to another thing.

In C++, you can't refer to the method fun() in the base class using the name fun, because fun is already being used to refer to fun() in the derived class. Overloading fun() in the derived class essentially hides fun() in the base class.

Java has name hiding as well. If the base class has a variable or static method with a particular name, and the derived class declares a variable with the same name or a method with the same signature, it will hide the variable/static method in the base class.
 
sinatra roger
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
Name hiding refers to that you can't use a name to refer to one thing, as it's already being used to refer to another thing.



thats what i wanted.thanks again
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Stephan

I think in your last post, may be not intentionally, but wrote that you can override static methods, which you can't..
Static methods are accessed only through class name. So there is no point of overriding them, because they will be called based on the Reference of the object....
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RohitJain Rj wrote:Hello Stephan

I think in your last post, may be not intentionally, but wrote that you can override static methods, which you can't..
Static methods are accessed only through class name. So there is no point of overriding them, because they will be called based on the Reference of the object....


Read it again. I don't see overriding being mentioned anywhere, I do however see name hiding.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Praveen
O Yes... Didn't actually noticed that...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!