Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding / Overloading method???

 
Heinz Wu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone tell me that if I am given the following:
class Foo {
String doStuff(int x) {return "hello";}
}
Is it legal to put
protected String doStuff(int x) {return "Hello";}
in a subclass of Foo?
my answer is illegal, because overriding method must not have a more trstrictive access modifier. Am I right or wrong?
Thanks
[ May 04, 2004: Message edited by: Heinz Wu ]
[ May 04, 2004: Message edited by: Heinz Wu ]
[ May 04, 2004: Message edited by: Heinz Wu ]
 
Peng Fan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,Heinz Wu:
i do not think the answer you got is correct,however those ideas which makes you reduced the conclusion are seemingly wrong.what make you to be caught in a dilemma,i think,is you can not make a clear distinguish between the access modifier default and protected,default has package access,protected have subclass access as well as package access.Do i made myself clear?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can not make a clear distinguish between the access modifier default and protected,default has package access,protected have subclass access as well as package access
But a very clear distinction between them exists - default is more restrictive. You said so yourself.
So, Heinz, you got default and protected backwards. A subclass could modify the overridden method with protected.
A good way to determine answers to questions like these is to simply write a simple example program and see what happens when compiling and running it.
[ May 04, 2004: Message edited by: Dirk Schreckmann ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic