• 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

I do not see a constructor, why the method is called?

 
Ranch Hand
Posts: 606
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
hi,
the output of the above code is

Hello world
Salut Fred
Hola, mundo



I noticed that inside the main class:

(that i substituted with String[] args as I never seen this "String...args" with dots and not with double squares ) there is a call to the method sayHello(), inside there are three classes. Till this should be fine, I am studying a tutorial on anonymous classes and it is clear. I do not understand why calling from the main myApp.sayhello() the output call the line:

System.out.println("Hello " + name);

I mean this sysout i see as first line in the console it is inside a method called

greetSomeone()

that is called inside another method called

greet()

. The point is: who called this greet method??? the main class call myApp.SayHello() not myApp.SayHello().greet() !!!

please do not read the following if it is already clear, really sorry if I am a bit long.
I am really confused so far I saw this behaviour of when there is a constructor, so that when an object is created and instantiated also without a call.. the compiler immediately execute the orders inside the constructor, but in this case who call the Sysout by mean of greet?
Of course there is an interface, so I guess that this behaviour is related to this interface, but as far as I know an interface is only a method to refer to a characteristic in OO that is not present in the super class, so that as it is not C++ every subclass can inherit one superclass. so that an interface does not execute methods but only establishes a contract that the method inside it MUST to be implemented, but still i do not see any specific call to make the compiler executing greet()--> that executes greetSomeone that executes in the end the sysout.

THANKYOU
 
Giovanni Montano
Ranch Hand
Posts: 606
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Moved to Beginning Java for you.


thanks Bear!
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think somebody asked a very similar question earlier this week. Look here.
Remember that every class has a constructor; if you don't write one, the compiler will add a default constructor for you. That looks like this:-So you can call a constructor and that enables you to create an object.
Once you have the object, you can call methods on it.
 
Giovanni Montano
Ranch Hand
Posts: 606
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I think somebody asked a very similar question earlier this week.(CUT .


i had to run a search in the forum before, effectively the link you sent me replies to my question.
so interfaces are not just a contract that must be implemented but they execute direcly the methods in the subclasses that implement them

cool to know! another piece of the puzzle goes to the right piace in my mind. thanks and sorry again
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
And I don't think you have anything to apologise for. Lots of people find the wrong forum.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic