• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriden method

 
sheri
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoa !! That's a good one.
I'm loving every ride in this Java Rodeo.
Ride on.
Sandeep
 
bala_chocos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3) Static will not compile. Please see that class Parent is defined as static class.
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LoveNPeace is right
Correct version is:

Sorry for my mistakes and delay, I should have replied earlier�
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic