Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

overriden method

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoa !! That's a good one.
I'm loving every ride in this Java Rodeo.
Ride on.
Sandeep
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LoveNPeace is right
Correct version is:

Sorry for my mistakes and delay, I should have replied earlier�
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic