• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JWhiz Question

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen when you attempt to compile and run the following code?
<code><pre>
class Base
{
private void amethod(int iBase)
{
System.out.println("Base.amethod()");
}
}
class Over extends Base
{
public static void main(String argv[])
{
Over o = new Over();
int iBase = 0;
o.amethod(iBase);
}

public void amethod(int iOver)
{
System.out.println("Over.amethod()");
}
}
</pre></code>
Answer:
D is correct(Output of Over.amethod). Please note that method amethod() has been overridden. Here a private method is being overridden to public. The overridden method can't be more private than the original method, but in the above case its overridden correctly. Thus its perfectly valid. The output will be Over.amethod().
Is this correct? Not questioning the answer, but the explanation.
[This message has been edited by Jim Hall (edited December 17, 2001).]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The explanation is wrong, private methods can't be overridden.
From JLS 8.4.6.1


An instance method m1 declared in a class C overrides another method with the same signature, m2, declared in class A if both
1.C is a subclass of A.
2.Either
m2 is non-private and accessible from C, or
m1 overrides a method m3, m3 distinct from m1, m3 distinct from m2, such that m3 overrides m2.
...


There is no overriding in this case since you create and object of type Over and invoke the method amethod() which is defined in Over on it. If you had created the object o like this:
Base o = new Over();
then you would get a compilation error stating that amethod has private access in Base. Polymorphism is not used at all in this example, so there is no overriding...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 17, 2001).]
 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a simple question about Overriding basics.Remember when ovveriding a method,overrided method(the one which is in child class) should not be more restricted than the original method(the one in Parent class).Rule is like this,
private--->default--->protected--->public
Following above rule, if original method(in Parent) is default then overrided method(in Child) must be private or defualt or protected or public, i.e less restrictive or same restrictive.
Take another e.g
If original method(in Parent) is default then overriden method must be default or protected or public i.e less or same restrictive but can't be private, which is more restrictive.
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic