Interfaces and instantiation

I read a book about Java (Sams Teach Yourself Java in the 21-day 6th edition) and I have a question.

The book says:

Interfaces cannot be created : a new one can only create an instance of a non-abstract class .

Then it goes on and says a paragraph or so on that you can declare a variable like an interface, for example.

Iterator loop = new Iterator();

Isn't that instantiating an interface as we use new?

+4
source share
3 answers

The second declaration is incorrect:

Then it continues and says a paragraph or so on: “You can declare a variable of an interface type, for example. Iterator loop = new Iterator();"

Iterator loop;, , Iterator, , Iterator , Iterator.

EDIT:

. 5-. :

, , , . , : Iterator loop = new Iterator() , , . , Iterator Iterator, , : hasNext(), next() remove().

Fantastic! 6- ! ...

+5

. , , , . , Runnable, :

Runnable instance = new Runnable() {
   @Override
   public void run() {
      ...
   }
};

Iterator 3 , : next(), hasNext() remove().

+4

: "" "", "Area" getParams() calculateArea(),

public static void main(String[] args) {
    Area area; //no direct instatiation
    Rectangle rect = new Rectangle();
    Circle circ = new Circle();
    area = rect;//assign as another object; indirect instantiation
    area.getParams();
    area.calculateArea();
    area = circ;
    area.getParams();
    area.calculateArea();
}

. , , . , .

0

All Articles