• Post Reply Bookmark Topic Watch Topic
  • New Topic

quick<stupid object> question: implements and method inside the interface  RSS feed

 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear ranchers
if i call a class that implements an interface the method inside the interface will be triggered automatically by the compiler or happens only in the observer pattern?
( hope is understandable, i keep simple to be surr the message came across, a typical example would be a listener on a button, or a collection that calls comparator)

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't call a class. You call a method on an object.
The method is called when you pass a message to it. A Listener on the button listens for Events occurring over it. When an action event occurs, for example if you click the button, that Event travels through all the Components in that particular location until it finds a Listener. Then that Listener consumes the object and fires its action performed method. That Event passes a message to the action performed method telling it to execute.
System.out.println(123); passes a message to the println method telling it to execute, with the information 123.
If there are no Listeners, the Event disappears into cyber‑limbo never to be seen again.

The compiler does not call methods; it simply checks that they are suitable to be called. If you call println with two parameters it will complain because println requires one parameter.

In the observer pattern, the object being observed calls methods in other things whenever its state changes as the observation requires.

And there is nothing stupid about that question.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a sort method, there is a Comparator call, something on the lines ofSo you see, the Comparator's methods are called directly in the code.
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You don't call a class. You call a method on an object.
The method is called when you pass a message to it. A Listener on the button listens for Events occurring over it. When an action event occurs, for example if you click the button, that Event travels through all the Components in that particular location until it finds a Listener. Then that Listener consumes the object and fires its action performed method. That Event passes a message to the action performed method telling it to execute.
System.out.println(123); passes a message to the println method telling it to execute, with the information 123.
If there are no Listeners, the Event disappears into cyber‑limbo never to be seen again.

The compiler does not call methods; it simply checks that they are suitable to be called. If you call println with two parameters it will complain because println requires one parameter.

In the observer pattern, the object being observed calls methods in other things whenever its state changes as the observation requires.

And there is nothing stupid about that question.

thank you Sherif to be so consistent and helpful in this tough ranch;) I will come back tomorrow as I need to do several quotes, now my generic Collection has only sleeping objects sorted inside the harsh law of the angle chevron brackets that make java safest to me, so as I am implementing the interface EuropeTime i must implement the sleep() method so I invoke severalmilions of milliseconds my body event thread to sleep(). thanks
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You don't call a class. You call a method on an object.

true thanks, important doctrinal precisations

Campbell Ritchie wrote:The method is called when you pass a message to it.


so if I have a object.setlistener the message( parameter to the method) is an event, namely usually is the famous this
usually in well designed patterns the parameter inside set listener is passes inside a setter that sets the parameter passed inside the class es
setlistener(ListenerInterface listener){
this.listener=listener}

after the listener( inside the class where is setListener) is instantiate automatically the interface is called and having the interface a method contract this method MUST be implemented in the calss that extends polymorphically the interface!

Campbell Ritchie wrote: A Listener on the button listens for Events occurring over it. When an action event occurs, for example if you click the button, that Event travels through all the Components in that particular location until it finds a Listener. Then that Listener consumes the object and fires its action performed method. That Event passes a message to the action performed method telling it to execute.
System.out.println(123); passes a message to the println method telling it to execute, with the information 123.
If there are no Listeners, the Event disappears into cyber‑limbo never to be seen again.

interesting never thought about stack and heap, i reckon is the heap as there is an object called, and the stack for the part of the variable instantiate and the actions inside the method setListener

Campbell Ritchie wrote:The compiler does not call methods; it simply checks that they are suitable to be called. If you call println with two parameters it will complain because println requires one parameter.

system.out.println is not final I coudl overwrite with a contructor that accept two parameters, if I am learning well..

Campbell Ritchie wrote:In the observer pattern, the object being observed calls methods in other things whenever its state changes as the observation requires.

And there is nothing stupid about that question.
because the observer is not knew from the view, thank you i love the quote "there are not stupid question" is something that goes deeper than a factorial recursion I guess.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:so if I have a object.setlistener the message( parameter to the method) is an event

No, its a Listener. Some Observer patterns do allow a Listener to specify what they're "interested in" though; and those may well be event types.

interesting never thought about stack and heap...

And I suggest you don't. At least, not yet. The stack and heap are simply memory implementations that really have very little to do with you as a programmer. And furthermore, you have no control over how the JVM uses them.

Create objects; use them; let the garbage-collector clean 'em up. It's as simple as that.

system.out.println is not final I coudl overwrite with a contructor that accept two parameters...

You could certainly subclass PrintStream and then override println(), but I don't see much future in it. I have to admit, I haven't read the entire thread, but I'm wondering why you think you might want to do this?

Winston
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Create objects; use them; let the garbage-collector clean 'em up. It's as simple as that.

thanks won't be my priority from now on.

Winston Gutkowski wrote:
system.out.println is not final I coudl overwrite with a contructor that accept two parameters...

You could certainly subclass PrintStream and then override println(), but I don't see much future in it. I have to admit, I haven't read the entire thread, but I'm wondering why you think you might want to do this?

any practical utility, just I am terribly curious to know how java works, I do not have strong knowledge of architectures, and with java is not required still I want to " be the compiler" and understand how it works everything chunk by chunk.

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote: . . .
so if I have a object.setlistener the message( parameter to the method) is an event, namely usually is the famous this
usually in well designed patterns the parameter inside set listener is passes inside a setter that sets the parameter passed inside the class es
setlistener(ListenerInterface listener){
this.listener=listener}
. . .
No. Classes do not have setListener methods. They have addXYZListener methods, but the message is, “add this Listener.” Winston has already told you.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote: . . .
interesting never thought about stack and heap . . .
What have stack and heap got to do with it? In some implementations, e.g. AWT, much of the passing of events is done by the operating system. In Java8 you can pass a λ instead of a Listener object reference, so who knows where that will be.

As Winston says, there may be different behaviours between different platforms and versions.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote: . . .
system.out.println is not final I coudl overwrite with a contructor that accept two parameters, if I am learning well..
. . .
Constructor? No, you cannot override that method with something which takes two parameters, you would overload it.But, as Winston says, why? You cannot extend System.out because that is not a class. It is an object reference.
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Giovanni Montano wrote: . . .
so if I have a object.setlistener the message( parameter to the method) is an event, namely usually is the famous this
usually in well designed patterns the parameter inside set listener is passes inside a setter that sets the parameter passed inside the class es
setlistener(ListenerInterface listener){
this.listener=listener}
. . .
No. Classes do not have setListener methods. They have addXYZListener methods, but the message is, “add this Listener.” Winston has already told you.

thank you, I think sometimes is not a good idea to study android and java at the same time, also if I have to recognize that studying them togheter I understand better and better java how it works.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Android is a form of Java, so there is nothing wrong with studying both. I think you are trying to go too fast, however.
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Android is a form of Java, so there is nothing wrong with studying both. I think you are trying to go too fast, however.



thank you to come back on this subject, I had long incertitude before studying the two topics togheter, several things overlapse, different methods, threads, activity, and also I recognize i am trying to go too fast.
studying wildcards on generics after roughly 4 months I started with java and android can be confusing. But the point is Java is my love( i am neapolitan so I like the cup of coffee that symbolize it my financial knowledge add synergy to Java to work in a corporative environment but, Android is the best way to step inside a not really easy market for old aged (I am 40) newcomer, so to put a foot in the door and make java my full job, I can study android easier publishising some small app in google play to enrich my CV, but I stop here as I am going super OT
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go to our Android forum and try your hand at the book promotion. Good luck with it
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Go to our Android forum and try your hand at the book promotion. Good luck with it

done it, thanks, understanding WHY some command are different, help to stick the concepts inside the mind, and make more satisfied my curiosity, I think as I want to know everything I would be more a C programmer, but Java is just great because is safe, and allow me to relax a bit when i do coding, reducing run time exceptions.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote: . . . allow me to relax a bit when i do coding, reducing run time exceptions.
Not sure I understand that.

C does not have runtime Exceptions. If anything goes wrong in C, you get an error code, which you have to query later, or a segmentation fault.
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Giovanni Montano wrote: . . . allow me to relax a bit when i do coding, reducing run time exceptions.
Not sure I understand that.

C does not have runtime Exceptions. If anything goes wrong in C, you get an error code, which you have to query later, or a segmentation fault.

I did not know, thank you to share that, I am definetely wrong.
Anyway I guess the C is a more low level language that allow really to act on registers, and offer in place of the security of the java code, a lot of flexibility that allow to "tweak" more the code,
for instance I read that languages C derived as C++ allow the so called deadly diamond of death, using instead of interfaces 2 different classes.
Anyway I have to admit sherif that I am not entitled to speak about languages, because my only experience of C are some line of code I did ages ago.
But still to be honest I like the feeling of power one can have to program operative systems, or I reckon the same java language probably is made in C, understanding everything that happens between compiler and architecture makes me really curious, and maybe one day I will be close there, for instance i remember than in C is possible to use assembly libraries.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:But still to be honest I like the feeling of power one can have to program operative systems, or I reckon the same java language probably is made in C...

Possibly originally, but not these days. Indeed, I believe there are compilers, and even JVMs written entirely in Java.

understanding everything that happens between compiler and architecture makes me really curious, and maybe one day I will be close there, for instance i remember than in C is possible to use assembly libraries.

Yes, but if you're really intersted in Java, then it's probably a backward step. Assembler really isn't used that much any more, except maybe for really low level stuff like device drivers.

Winston
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Giovanni Montano wrote:But still to be honest I like the feeling of power one can have to program operative systems, or I reckon the same java language probably is made in C...

Possibly originally, but not these days. Indeed, I believe there are compilers, and even JVMs written entirely in Java.

Winston, Sorry for the delay, this is unbelievable to me.. compiler entirely written in Java! SO i reckon there are some special API that go at a registry level, or maybe is a specific mixture of Java and C.
anyway I really look forward to advance in this astonishing field, maybe when I will land in more advanced topic like the land of the java relfextion i will be able to grasp better this really interesting topic,
thank you
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:Winston, Sorry for the delay, this is unbelievable to me.. compiler entirely written in Java! SO i reckon there are some special API that go at a registry level, or maybe is a specific mixture of Java and C.

I think that's most normal; and it's called JNI - although I think it also allows you to call assembler routines (and possibly even machine code) directly.

anyway I really look forward to advance in this astonishing field, maybe when I will land in more advanced topic like the land of the java relfextion i will be able to grasp better this really interesting topic

Well, if this "low-level" stuff is really what rocks your boat, why not learn C or C++ directly? There isn't really much call for JNI in normal Java programming; and it also goes against the general "ethos" of the language, which is: Write once; run anywhere.

My advice would also be to steer clear of reflection until you're a lot further along. It's a bit of an aberration in Java, because the language is statically-typed; so it tends to be difficult to understand, verbose, error-prone, and SLOW. In many cases there are also other ways to do the same thing; so the general rule - even for experts - is: Keep it to an absolute minimum.

thank you

You're most welcome. And good luck.

Winston
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Well, if this "low-level" stuff is really what rocks your boat, why not learn C or C++ directly? There isn't really much call for JNI in normal Java programming; and it also goes against the general "ethos" of the language, which is: Write once; run anywhere.

because c programmers do not have shirts like this guy that starts a conference saying: do you have questions otherwise i go away?
http://youtu.be/9ei-rbULWoA
i like these nerds, not the financial guys that i met every day, java is fun and protect me from mistakes much more, this mean i can listen miles davis or bach while coding, what could i ask more? an arduina girlfriend?

Winston Gutkowski wrote:
My advice would also be to steer clear of reflection until you're a lot further along. It's a bit of an aberration in Java, because the language is statically-typed; so it tends to be difficult to understand, verbose, error-prone, and SLOW. In many cases there are also other ways to do the same thing; so the general rule - even for experts - is: Keep it to an absolute minimum.


this is what is called creativity, coding doing the same thing in different way is the maximum expression of creativity, only that marketing people are so busynto ask specification for not implementable ideas that they do not understand the beauty of coding, they do not know why a coder is happy when goes from uncecked exceptions to Solutions, the more fun and creative act an human being can make!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!