• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

anonumous class

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am moving this to Certification Study since it is not related to a specfic Mock Exam error.
Bill
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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/
 
sunilkumar ssuparasmul
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out this code. May be this can clear your doubt.


------------------
Velmurugan Periasamy
http://www.geocities.com/velmurugan_p/
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic