I
I
iscateli2021-12-30 19:05:35
Java
iscateli, 2021-12-30 19:05:35

Why are checkout fields called interface?

Bruce Eckel in his book "Java Philosophy", in several places calls the fields of a class an interface:

(p.238-239)

//:polymorphism/music/Music.java
//Объекты Note для использования с Instrument.
package polymorphism.music;
publicenumNote{
MIDDLE_C, C_SHARP, B_FLAT; //И т.д.
}


In the following example, Wind is a special case of an instrument,
so the Wind class inherits from Instrument:


//:polymorphism/music/Instrument.java
package polymorphism.music;
import staticnet.mindview.util.Print.*;
class Instrument{
public void play(Note n){
  print("Instrument.play()");
  }
}
///:~
//:polymorphism/music/Wind.java
package polymorphism.music;
//Объекты Wind также являются объектами Instrument,
//поскольку имеют тот же интерфейс:
public class Windextends Instrument{
//Переопределение метода интерфейса:
public void play(Note n){
  System.out.println("Wind.play()"+n);
  }
}

//:polymorphism/music/Music.java
//Наследование и восходящее преобразование
package polymorphism.music;
public class Music{
public static void tune(Instrument i){
//...
i.play(Note.MIDDLE_C);
}

public static void main(String[] args){
   Wind flute = new Wind();
   tune(flute);//Восходящеепреобразование
}

}  /*Output:
Wind.play() MIDDLE_C
*///:~


The Music.tune( ) method takes a reference to an Instrument, but the latter can also point to an object of any class that derives from Instrument. In the main( ) method, a reference to the wind object is passed to the tune( ) method without explicit conversion. This is fine; The interface of the Instrument class must also exist in the Wind class, since the latter was inherited from Instrument. Upcasting from Wind to Instrument can "narrow" this interface , but does not make it "smaller" than the full interface of the Instrument class.


(p.244)
The Shape base class establishes a common interface for all Shape-derived classes—that is, any shape can be drawn ( draw( ) ) and erased ( erase( ) ) . Derived classes override this interface to implement unique behavior for each specific shape.


(p.260-261)
However, we know that upcasting is perfectly safe; the base class cannot have a "bigger" interface than the derived class, and therefore any message sent to the base class is guaranteed to reach the recipient.

....
Polymorphism means "a variety of forms". In object-oriented programming, the base class provides a common interface , and different versions of dynamically bound methods provide different forms of using the interface.


Why does the author call class fields an interface, and how is this different from another definition of an interface that uses the function word interface? Why are these things called by the same word?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
ComodoHacker, 2021-12-30
@iscateli

Here the term "interface" is used in a broader sense:

Interface (from English interface) - the boundary between two functional objects, the requirements for which are determined by the standard [1]; a set of means, methods and rules of interaction (management, control, etc.) between the elements of the system

The fields (and methods) of a class are a way for other parts of the program to interact with objects that are instances of that class.

V
Vasily Bannikov, 2021-12-30
@vabka

Well, here we have such an illogical language, leather bags.
An interface is not only what we declare with the "interface" keyword, but also a collection of all public members of the class - the so-called "interaction interface"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question