• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Regarding method overloading and overriding  RSS feed

 
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 ?
 
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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 ?
 
Java Cowboy
Sheriff
Posts: 16084
88
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!