• 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:

private method...

 
Ranch Hand
Posts: 435
  • 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
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
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
remember you are not overriding the private method of the super class, you are shadowing it!
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roger That!!!
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
}
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic