• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overriding

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question from Jaworski's quiz.
Which of the following are true about method overriding?
A. The overriding and overridden methods must have the same name, argument list, and return type.
B. The overriding method must not limit access more than the overridden method.
C. The overriding method must not throw any exceptions that may not be thrown by the overridden method.
D. The overriding method may not be private.
Answer A, B, C, D. All are true.
I agree with all but choice D.
This code below compiled just fine. So wouldn't answer D be wrong. Maybe I'm overlooking something with the question. Could someone shed some light on this. Thanks.
class A {
private int aMethod(int i){
return 1;
}
}
class B extends A {
private int aMethod(int i){
return 2;
}
}
 
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
D is correct.
When you made the method private in your subclass, you made it overload the method instead of override.
 
Rich Wright
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What makes aMethod in the subroutine overload aMethod in class A? It has the same return type, argument list and method name.
Overloaded methods must have different arguments.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Paul said. It's the fact that it's private.
Consider what 'private' means. The 'private' modifier limits the visibility of the method to the defining class. A 'private' method (or member, or inner class) is not visible to derived classes, classes in the same package or completely unrelated classes.
Any other method in class A may call A.aMethod, but no method in class B may call A.aMethod. Class B can't "inherit" A.aMethod, because it is marked as 'private'. Class B simply doesn't know that A has such a method.
In the example, class B defines a method which just happens to have the same signature as a private method in a superclass. But, that private method is completely unknown to class B, so the method in class B can't "override" the superclass method.
If two methods have the same name, but can't be interchanged, they are therfore "overloading" the name. These two methods can't be interchanged, because A.aMethod can only be called by a method of A, and B.aMethod can only be called by a method of B.
 
Rich Wright
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Frank, I understand now.
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic