Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding method overloading and overriding  RSS feed

 
h kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Can anybody give me the answer for this question..

1.my question is can we have the same or different access specifiers(like private or public for the methods definition) for the overloading and overriding?

2.can a method overloaded in its subclass ?
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by h kumar:
1.my question is can we have the same or different access specifiers(like private or public for the methods definition) for the overloading and overriding?

Overloading creates a completely new method that just happens to have the same name. Therefore you can do anything with it - except give it the same parameter list as the overloaded method.

For overriding you can only make a method more public. You can't override private methods (the sub class doesn't know anything about them, you'll redefine them instead), but methods with default access can become protected or public, and protected methods can become public. Public methods can't become protected though.

2.can a method overloaded in its subclass ?

Overloading can happen even in the same class because of what I said before.
[ October 08, 2007: Message edited by: Rob Prime ]
 
h kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
Thanks for your replay, as now i understand bit but still i ahve some doubt regarding overloading

as my question is yesterday i red in a book that we can overload a method in subclass also.. how is it possible ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overloading in a subclass:

Now class Sub has two overloaded versions of method(...): one that takes an int (inherited from Super) and one that takes a String.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!