It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes private method... 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 "private method..." Watch "private method..." New topic

private method...

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
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){
1) Compile time error complaining that Base.amethod is private
2) Runtime error complaining that Base.amethod is private
3) Output of "Base.amethod"
4) Output of "Over.amethod"

Ans : 4)
I cannot understand why the answer is 4)
I feel that since the method is private, it wont be accesible in a different class..
please help..
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2591

The method amethod(int) is available in the subclass as a public method and can be accessed with object of the subclass. Hence it will print -
So option 4 is the correct option.
- Manish
Mike Beaty

Joined: Dec 19, 2001
Posts: 20
remember you are not overriding the private method of the super class, you are shadowing it!

Mike Beaty<br />Sun Certified Programmer for the Java 2 Platform
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
since the method is private it cannot be accessed at all. as far as the subclass is concerned it does not even know that such a method is there. Only the class that owns the method knows about it.
so you cannot say that the concept of overidding is being applied here. NO there is no overidding involved here.
remember the example Bill gave as below. gives compile error. right but if you make the method in class B as private it will not be any error. because you are not overidding any more.
class A{
int get(int i){return 1;}
class B extends A{
long get(int i){return 1;}//line 1
[ January 27, 2002: Message edited by: mark stone ]
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Thx Mark I didn't knew it. Is it what mike call shadowing???
What would hapenned if we had
Base o = new Over();
instead of
Over o = new Over();
with amethod() being private.
I suppose it will give an error compilation, because amethod is invible to subclass.

By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

Originally posted by Younes Essouabni:
Is it what mike call shadowing???
I Hope it helped you!!!

Younes, you really should try get those sticky punctuation keys fixed.
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Roger That!!!
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172

remember you are not overriding the private method of the super class, you are shadowing it!

Arent we overriding the private method still in class Over when we say:
public void amethod(int iover)
System.out.println("over method")
It appears that we are indeed only accessing the parent's private method in the main() of the sub:
public static void main(String[] args)
Over o = new Over()
int iBase = 0
o.amethod(int iBase)
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
You can only override a method if the overriden method is inherited. Private instance methods are not inherited by the subclass, therefore you cannot override a private instance methods.
Static methods cannot be overriden at all. Static methods can be hidden if a declaration in the subclass has the same name and signature as one in the superclass. A Private static method is not inherited by the base class, so you cannot hide it in a subclass.

SCJP 1.4
I agree. Here's the link:
subject: private method...
It's not a secret anymore!