• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overriding a Method  RSS feed

 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope I'm in the right forum, I apologize in advance if this questions should belong in the "intermediate" group, but I don't think this is more of a beginners questions. Well here goes...I took this exam with the following question:
Which of the following methods can be legally inserted in place of the comment //Method Here?
class Base
{
public void amethod( int i )
{
}
}
public class Scope extends Base
{
public static void main( String argv[] )
{
//Method Here
}
}
1. void amethod( int i ) throws Exception()
2. void amethod( long i ) throws Exception()
3. void amethod( long i ){}
4. public void amethod( int i ) throws Exception
I chose answer #4 since "public" is less or equals to restrictive (in this case, equal to ) to the method that is being overriden, since the others would have a "default/package" modifier. Supposedly, the answer is #2 and #3. Can someone please explain why #2 and #3 would be correct?
Explanation is appreciated!
 
Greg Neef
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Which of the following methods can be legally inserted in place of the comment //Method Here?
class Base
{
public void amethod( int i )
{
}
}
public class Scope extends Base
{
public static void main( String argv[] )
{
//Method Here
}
}
1. void amethod( int i ) throws Exception()
2. void amethod( long i ) throws Exception()
3. void amethod( long i ){}
4. public void amethod( int i ) throws Exception
I chose answer #4 since "public" is less or equals to restrictive (in this case, equal to ) to the method that is being overriden, since the others would have a "default/package" modifier. Supposedly, the answer is #2 and #3. Can someone please explain why #2 and #3 would be correct?


#2 and #3 can be legally inserted because they are not overriden given they have a different signature. #4 can't be inserted because it throws a new exception and you can't do that in an overriden method.
[ September 11, 2003: Message edited by: Greg Neef ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to point out that, as written, the answer is "none of the above," because the comment is inside the main() method, and you can't define "nested methods" in Java. Otherwise, if you move the comment outside main(), Greg's answer is right on.
 
Greg Neef
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to point out that, as written, the answer is "none of the above," because the comment is inside the main() method, and you can't define "nested methods" in Java.

good point. hope that one isn't on the SCJP exam ( I am busy cramming for) or I might miss it again.
[ September 11, 2003: Message edited by: Greg Neef ]
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although at this point it really doesn't matter, since you can't have "nested methods" as Ernest pointed out, but what if these methods are outside of main???
So in this example, we are "overloading" a method. Don't they then have to be declared as "public"?
Greg, you stated that:
"#4 can't be inserted because it throws a new exception
and you can't do that in an overriden method"
but yet #2 throws the same Exception???
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about a cabinet full of dishes.
"Overriding" a dish means putting another identical dish on top of it (the overriding dish "rides" on top of the others, if you like.) The overriding dish has to be more or less exactly like the one below it, or it will hang over or otherwise not fit into the stack of dishes. Similarly, when a Java method overrides another, everything has to be the same: the return type, the arguments, and the exception signature. The only allowable difference is that the top dish -- I mean "method" -- on the stack can be more "visible" than the others; it can be public, even though the overridden one was not. If the lower-down dishes are public, the top one has to be, too.
Overloading a dish means putting another similar but different dish next to it. If two methods have the same name but different arguments, they are said to be overloaded. If the argument lists are different, then they're free to have different return types, exception signatures, and visibilities.

Overloaded methods are really completely separate, unrelated methods -- they just look similar to us because they use the same symbol for a name. They're a convenicne feature more than anything else. On the other hand, overriding a method involves specific mechanisms within the Java Virtual Machine; it's a formal concept.
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhhh...., got it. Nicely put!!! I can definitely relate with dishes since I hate cleaning them...LOL! Thanks for the explanation!
 
Tim Leung
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Base
{
public void amethod( int i )
{
}
}
public class Scope extends Base
{
public static void main( String argv[] )
{
//Method Here
}
}

1. void amethod( int i ) throws Exception()
2. void amethod( long i ) throws Exception()
3. void amethod( long i ){}
4. public void amethod( int i ) throws Exception

Ans 1,2,3 are wrong. It is because the method "amethod" in class "Base" is declared as public, "amethod" in class "Scope" cannot be declared with more restriction than its father class(Base). (default and private cannot be declared).Therefore method "amethod" in class "Scope" must be declared as public.

Ans 4 is also wrong, although amethod is declared as public, method "amethod" in class "Base" doesn't contain "throws exception".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!