This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriden method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriden method" Watch "overriden method" New topic
Author

overriden method

sheri
Ranch Hand

Joined: Sep 14, 2000
Posts: 43
public class Parent {
private void print(){
System.out.println(" From Parent");
}
public static void main(String args[]){
Parent p1 = new Parent();
p1.print();
p1 = new Chield();
p1.print();
}
}
class Chield extends Parent{
public void print(){
System.out.println(" From chield");
}
}
Output will be:
A. Compile error can't override the private method print()

B. From Parent
From chield
C. From Parent
From Parent

D. From chield
From chield
pls help me
my ans:B
ans given:C
help pls......
thanx
sherin
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Wow, good question!
I was also confused, I expected otput to be
From Parent
From chield
Here are my guess:
1. p1 has compile-time type of Parents, and run-time type of Chield
2. method declared as private cannot be overriden, Chield class just declares another method with the same name.
3. If referenece compile-time type is different from run-time type, only overriden methods will be envoked using run-time reference (Chield in your example)
I removed �private� modifier from print() method, and now output is
From Parent
From chield

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Sheri, that question was real fun !!
Mapraputa is correct. Please note that private methods are implicitly final. Therefore u cannot override them.
Hence, ans = Mapraputa's ans
-sampaths
Sandeep Potnis
Ranch Hand

Joined: Aug 18, 2000
Posts: 39
Whoa !! That's a good one.
I'm loving every ride in this Java Rodeo.
Ride on.
Sandeep
bala_chocos
Ranch Hand

Joined: Aug 28, 2000
Posts: 48
hi
iam having doubt .the overriding method access modifier
can be promoted to public .i read from one book. but how
it is [printing parent and parent . iam still confused
with mahpu answer explain me
geetha
Greenhorn

Joined: Jun 27, 2001
Posts: 15
Hi bala_chocos:
as private method can not be overriden. jvm does not look at to the child class method "public....." i.e here dynamic method dispatching is not happening(it occurs only when overiding happens).it is just coincidence of a child class to declare the method same as private method in the base class.
geetha
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Just to finish topic �which method cannot be overriden�:
1.private
sheri gave a good example
2. final

produces compile-time error: final method cannot be overriden
3. static

will compile, but Child's method is said to HIDE, not to override Parent's method.
If you try to call it with
Parent p1 = new Chield();
Output will be
From Parent
From Parent
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
3) Static will not compile. Please see that class Parent is defined as static class.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
LoveNPeace is right
Correct version is:

Sorry for my mistakes and delay, I should have replied earlier�
 
jQuery in Action, 2nd edition
 
subject: overriden method
 
Similar Threads
Overriding private method
Access control
Overriding
Question on Overriding
Overiding