• 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

Problems with Interfaces

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is inside code:



In other file:
 
Author
Posts: 282
11
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At present, your code sample has messed up curly braces. Fix those and it might be easier to see what you had in mind
 
John Losty
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


other file:
 
Simon Roberts
Author
Posts: 282
11
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because "j" is a member of the class MainActivity, and does not exist in the OnNewItemAddedListener interface.

In general, I strongly question the design of trying to reach into this thing in this way, but from a purely syntactic perspective, you should be able to cast the OnNewItemAddedListener to a MainActivity (if you're sure it is one!) and then, from the new expression, access the specific features of MainActivity.

E.g.



But please know this a really dreadful approach to any problem. Totally not "good OO". That, however, is a much bigger question, that we probably cannot address here...

HTH
Simon
 
John Losty
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is this even possible:

Is there any rule about this. Because onNewItemAddedListener and Activity are not connected in any way ?

If I wanted to get MainActivity's reference inside onNewItemAddedListener variable I would do simply downcasting(Because MainActivity extends Activity)
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Losty wrote:Why is this even possible:


Because activity is an instance of a class which implements OnNewItemAddedListener. Also, no need to cast - just specify onNewItemAddedListener = activity;
 
John Losty
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Activity doesn't implement OnNewItemAddedListener ?

Would this be correct?
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes - you're right -- I didn't notice that activity was an instance of Activity - you will need the cast - either to MainActivity or more specifically to OnNewItemAddedListener.

     onAttach(Activity activity)
 
John Losty
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again I am not asking how to fix the code but(let me repeat the question):

Why is this even possible:


Is there any rule about this. Because onNewItemAddedListener and Activity are not connected in any way ?
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
activity is a reference to an instance of MainActivity, and MainActivity implements OnNewItemAddedListener
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic