Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JWhiz Question

 
Jim Hall
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • 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).]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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).]
 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic