File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JWhiz Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JWhiz Question" Watch "JWhiz Question" New topic

JWhiz Question

Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
What will happen when you attempt to compile and run the following code?
class Base
private void amethod(int iBase)
class Over extends Base
public static void main(String argv[])
Over o = new Over();
int iBase = 0;

public void amethod(int iOver)
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

Joined: Aug 26, 2001
Posts: 7610
The explanation is wrong, private methods can't be overridden.
From JLS

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.
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...
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 17, 2001).]

[Blog] [Blogroll] [My Reviews] My Linked In
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
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,
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.
Count the flowers of ur garden,NOT the leafs which falls away!

Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining
I agree. Here's the link:
subject: JWhiz Question
It's not a secret anymore!