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 Base { private void aMethod() { System.out.println("I am in base "); } public Base() {} public static void main(String args[]){ //case 1 Base b = new Base(){ void aMethod(){ System.out.println("inside main of base b"); } }; b.aMethod();
// case 2 new Base(){ void aMethod(){ System.out.println("inside anonumous of base "); } }.aMethod(); } } In the above code since i ahve declared aMethod in base as private , the base object b when calls aMethod it prints that I am in base where as when i tried to do the same in the next case i.e case 2 , i am getting "inside anonumous of base " . CAN SOMEBODY PLZ HELP ME ------------------ "Winners don't do different things They do things differently"
"Winners don't do different things<br /> They do things differently"
bill bozeman
Ranch Hand
Joined: Jun 30, 2000
Posts: 1070
posted
0
I am moving this to Certification Study since it is not related to a specfic Mock Exam error. Bill
Velmurugan Periasamy
Ranch Hand
Joined: Nov 09, 2000
Posts: 95
posted
0
In both cases, you are creating a new class. But in Case 1, you're invoking the method via a Base class variable. So this calls the private method in the Base class since private methods are not inherited and not available to the sub-classes and dynamic binding is not needed. Essentially, this call is resolved at compile-time. In case 2, you're invoking the method directly on the new class. So the implementation in the new class is called. But remember, in both cases you're not overriding the method. You are just defining a brand new method that happens to have the same signature. Make the method in Base class not private and test this out. HTH ------------------ Velmurugan Periasamy http://www.geocities.com/velmurugan_p/
------------------------------------------------------------------------------------<BR>Velmurugan Periasamy<BR>Sun Certified Java Programmer for Java 2 Platform<BR>Sun Certified Web Component Developer for J2EE platform<BR>Sybase Certified EAServer Developer (similar to jCert level 3)<BR>------------------------------------------------------------------------------------<BR>Study notes for Sun Java Certification<BR><A HREF="http://www.geocities.com/velmurugan_p/" TARGET=_blank rel="nofollow">http://www.geocities.com/velmurugan_p/</A><BR>------------------------------------------------------------------------------------
sunilkumar ssuparasmul
Ranch Hand
Joined: Dec 13, 2000
Posts: 142
posted
0
IF i remone private its working properly but my questuion y is it so sucjh a strange behaviour when i declare base method 2 be private ------------------ "Winners don't do different things They do things differently"
Sam Zheng
Ranch Hand
Joined: Nov 29, 2000
Posts: 61
posted
0
HTH's answer doesn't make sense to me. It seems that this runs against all the rules about dynamic binding stuff. Is private method doesn't participate in dynamic binding? Is private method treated as fields in this case? Can anyone shed some light? Thanks!
bill bozeman
Ranch Hand
Joined: Jun 30, 2000
Posts: 1070
posted
0
Velmurugan said it correctly. If a method is private, it is only available to that class and not any subclasses. Therefore, when you say b.aMethod it is going to run the aMethod of the Base class and not do dynamic binding, because it is a private method. If it was protected, public or "friendly" then it would act as normal polymorphic behavior. Know in the second example, you are creating an anonymouse inner class inside of main, and then when you said .aMethod() you are not saying b.aMethod(); and Calling b which is of type Base, you are just saying aMethod(); so you get the aMethod of the anonymous class. Bill PS. HTH stands for Hope that Helps
Velmurugan Periasamy
Ranch Hand
Joined: Nov 09, 2000
Posts: 95
posted
0
Check out this code. May be this can clear your doubt.