This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
posted
0
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
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
LoveNPeace is right Correct version is:
Sorry for my mistakes and delay, I should have replied earlier�