• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private method...

 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen when you attempt to compile and run the following code
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");
}
}
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..
Sonir
 
Manish Hatwalne
Ranch Hand
Posts: 2595
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 -
"Over.amethod".
So option 4 is the correct option.
HTH,
- Manish
 
Mike Beaty
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember you are not overriding the private method of the super class, you are shadowing it!
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Correct???
 
Junilu Lacar
Bartender
Posts: 7480
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Younes Essouabni:
Is it what mike call shadowing???
Correct???
I Hope it helped you!!!


Younes, you really should try get those sticky punctuation keys fixed.
Junilu
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger That!!!
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic