• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How do I call an interface method (checkMate()) overridden in an inner class?

 
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help





 
Marshal
Posts: 74020
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags to your post (and remove some tags you didn't seem to be using). Always use the code tags: don't thay make it look better

I can't see anything wrong with your code. What happens when you run it, and what did you expect to happen?
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thank you!!

I am sorry, I was not aware about code tags.

Sir, this code compiles and runs properly but I want to call "checkMate()". I tried but I could not do it.

Kind Regards,
Abhimanyu
 
Campbell Ritchie
Marshal
Posts: 74020
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your method doesn't call chekMate anywhere. Try the following line in the Argue#getBackToWork method:
ci.checkMate();
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great!

It worked. But I could not understand why we are calling it in class Argue.

Kindly explain it to me.
 
Campbell Ritchie
Marshal
Posts: 74020
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are passing a reference to a CI instance to the getBack method. That is the only place that reference can be used, since it is an anonymous class and you are not keeping a reference anywhere in the other class.
I am going to try some changes in Java8, like writing this
a.getBackToWork(() -> System.out.println("Used a \u03bb expression"));
instead of lines 6-13.
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the explanation.

You are trying to use "Lamda Expression" with it. Right?
 
Campbell Ritchie
Marshal
Posts: 74020
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I couldn't get it to work.
 
Campbell Ritchie
Marshal
Posts: 74020
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trying to use a lambda and failing
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh!

But that's OK. You make it work for me. Thank you so much!!
 
Campbell Ritchie
Marshal
Posts: 74020
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I am daft! I was trying the λ without the ci.checkMate() call. That is why it wouldn't work!
Start with a dir (Windows®) or ls (*nix) instruction at the command line/terminal. Count the .class files. Delete all the .class files

Make the following two changes:
Add the ci.checkMate(); line as before
Change lines 6-13 in Implement to read
    a.getBackToWork(()-> System.out.println("Working with a \u03bb"));
Compile and run with Java8 only. Now count the .class files. See the difference?
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After making the changes, the output is:-

Working Anonymously
Working with a ?
Working

Sir, I tried to use code tags but failed. Could you please guide me how to use it?

Regards
Abhi
 
Marshal
Posts: 22450
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read UseCodeTags (that's a link).
 
Campbell Ritchie
Marshal
Posts: 74020
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhimanyu Jain wrote: . . .
Working Anonymously
Working with a ?
Working
. . .

I got that, too. \u03bb is λ
So obviously the lambda does work after all.
It didn't seem to make much difference whether I added the @FunctionalInterface annotation to the interface or not.

As Rob says, the link tells you how to use code tags. Push the code button, then put the code after the ] and before the [/code.
Or highlight the entire code block and push the code button.
There is more about code tags here and here.
 
Campbell Ritchie
Marshal
Posts: 74020
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I presume you noticed the absence of the $1.class file?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic