Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Overloading in Subclasses

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going through the book by - K&B and on page 306 where Overloading is discussed, it says that -
Overloaded methods can change the access modifiers.
but the program below is certainly wrong and does not compile
=================
public class Hello extends Hello1
{
protected void load(float x){}
public static void main(String args[])
{
Hello h = new Hello();
}
}
class Hello1
{
public void load(float x){}
}
=================
** Compiling it with JDK1.4 give the error as
Hello.java:5: load(float) in Hello cannot override load(float) in Hello1; attempting to assign weaker access privileges; was public
protected void load(float x){}
^
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you're overriding, not overloading.
However, overridden methods can change access modifiers, but only to make the method more accessible. Therefore, it's fine to override a protected method with a public method, but not vice versa.
Check out the JLS, §8.4.6 Inheritance, Overriding, and Hiding for more information.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sopal
Maybe this may help
=================
** Compiling it with JDK1.4 give the error as
Hello.java:5: load(float) in Hello cannot override load(float) in Hello1; attempting to assign weaker access privileges; was public
protected void load(float x){}
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is not overloading.u r trying to overriding the same method.
overloading must have the different types of the arguments.since u r trying
to overridng the method,the method in the sub class cannot be more private
the in the super class,(u cannot reduce the access control).
private->default->protected->public
this is the restriction of the access speifier
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!