• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

private methods

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Have a look at the following program:
class superclass
{private static void pri()
{System.out.println("super");}
void call(){pri();}
}
class subclass extends superclass
{
private void pri()
{ System.out.println("sub");}

public static void main(String a[])
{subclass p1 = new subclass();
p1.call();}
}
The o/p: super.
But shouldnt it print sub since private methods cant be overridden.

Thanks!

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi avn,
I think you know the answer. You just got a little confused over here. Yes, you are right, the private methods are not overridden. So, the answer is right, since pri() is not overridden. If it had been overridden, "sub" would have been printed !!!
Regards,
Aman
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! for the reply.But if i remove static from
the superclass method.Then, sub is being printed.This
is so because since super method is not available to
subclass.Hence overridden is not possible.So, subclass
calls its own method .Please correct me .
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
avn:
When you remove the static modifier from the super class method, the code still prints
super
Please check.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks!thomas.U are right.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK the foll cases are possible
1) When pri() in superclass is both static and private.
ans: superclass.
p1.call() calls the call() method in the superclass as the subclass does not override it. Now in the call method a call to pri() is made, which is a private method and therefore cannot be overridden (so the pri() method in subclass is a diff method from teh one called in the call() method). Therefore the pri() meth in superclass is chosen.
2) When pri() is not static but private
again the same result
ans: superclass
3) When pri() is not staic and not private
ans: sub
First of all in this case U would have to make pri() in subclass also non private. Else u would get a compiler error - This is bcoz when pri() is non private in the superclass it is visible to the subclass.. and clearly subclass also has a pri() method - implying it ought to be the overridding one. But a overriding method cannot be more private- so pri() in subclass will have to be not private.
Now why do we get ans as : sub.
The reason is bcoz of the concept that it is the underlying object of the reference that determines which method will be called.
here:
sublass p1=new subclass();
reference to the object is : p1 (which is of the type subclass)
underlying object : is also of type subclass
as p1 is both a reference of the subclass and the underlying object is also of the subclass - it calls its own pri() method which is in this case is visible as its non private.
hope this has made it clear!
[This message has been edited by eskay kumar (edited August 19, 2000).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic