Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic