There was a question that is associated with the creation of the interface.

The task is as follows: create your own interface with one method and implement it, and an internal class inside this method that will return a pointer to this interface. Sorry for the stupidity of the question, but what should be done? After all, as far as I know the interface is a class with methods that are not described in it? If so, how to understand "will return a pointer to this interface"?

If possible with an example, please.

  • And why are you asking here? Ask the one who gave the task. - andreycha
  • 3
    Is it about Java 7 or Java 8? - Nofate
  • one
    @Nofate even reminded me of my question)) ru.stackoverflow.com/questions/491171/… .......... so, Muscled Boy if it's java8 - then everything is possible) - Alexey Shimansky
  • @ Alexey Shimansky, How can you understand "everything is possible"? - Muscled Boy
  • @MuscledBoy, it means - it is possible that in Java 7 it was impossible) - Nofate

1 answer 1

In Java 8, default methods appeared in the interfaces, and you can crank something like this:

 interface Foo { // интерфейс default Foo foo() { // его метод foo с реализацией по-умолчанию, который должен вернуть экземпляр интерфейса class Bar { // локальный класс Bar в методе foo public Foo bar() { // метод bar класса Bar return new Foo() { // возвращаем экземпляр анонимной реализацию интерфейса }; } } return new Bar().bar(); // создаем экземпляр локального класса Bar, вызываем метод bar и возвращаем то, что он вернул } } public class Baz implements Foo { // класс Baz, унаследованный от Foo (а значит у него есть метод foo) public static void main(String[] args) { // отсюда стартует программа Baz myBaz = new Baz(); // создаем экземпляр класса Baz Foo myFoo = myBaz.foo(); // вызываем на нем метод foo ,который вернет нам анонимный экземпляр Foo, созданный методом bar класса Bar } } 

Comments:

Why do you put in front of the bar and foo "Foo" methods?

This is the type of return value. Any method must either return something or be void .

and the last line is essentially the creation of an instance of "Foo" - "myFoo"?

Yes exactly.

create an interface with one method and implement it, what's this? in this case, is its implementation the creation of a local class and the return of its instance? or by "implement" it means the interface, and does the class "Baz"?

Creating a single method interface Foo is the declaration interface Foo and Foo foo() .
You can implement it either in a specific class ( Baz ) or in the default method in the interface itself.
In this case, its implementation is the default implementation in the default Foo foo() method.
The Baz class, which implements the Foo interface due to the default implementation of the foo() method, is needed so that you can create an object to which the foo() method can be called.

  • it was about 8 java - Muscled Boy
  • can I ask you to explain the code, frankly speaking, a little confused. - Muscled Boy
  • and what did you want to say saying “you can turn this up”, I’m just a beginner, so don’t judge strictly for nonsense and, perhaps, excessive simplicity of the question - Muscled Boy
  • one
    Anonymous, so we do not create a separate named class. - Nofate
  • one
    The point is that we in the interface can specify the implementation of the method. And the class that implements the interface may no longer define this method if it is not required. - Nofate