• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is this - instance of an interface?  RSS feed

 
Biniman Idugboe
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, the ArrayList class implements the List interface.


So, is test an instance of the List interface, recalling that the ArrayList class has already added method bodies to the body-less methods of the List interface? 
Also, what are the reasons for using  instead of
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look up ArrayList in the documentation, you will find it has three superclasses including Object and implements six interfaces, so you can say it is an instance of all nine of those types, as well as ArrayList itself. Up to Java7, you could say something is an instance of List if it says it implements List and either is an abstract class or it has full implementations of the twenty‑something methods of the List interface. [In Java8 more methods were added, but that is a different story.] Also, you can look through the List documentation and it will give you the names of the classes which implement that class, not including classes somebody outside Sun or Oracle has written. 23 of them, if I counted it right.
If you declare your objects as List, you are not restricting yourself to an ArrayList. It can therefore be changed to a different sort of List easily enough.
You can get problems with some methods.  It is better to be non‑specific about the types you have. If you have a bird and don't know what sort, it is hazardous to try declaring it as a sparrow. Even better, if it is a flying animal and you have a flying animal interface, you can assign a bird to it or a bat or a bee or a fly. So be non‑specific. You know you want a List, so don't specify a particular type of List. There are many methods which work on that principle. Look at something like Collectors#toList(); it returns a Collector which creates a List, not an ArrayList. Or Collections#unmodifiableList(). You have to assign that to List, not ArrayList.
Actually, if you look hard at those two methods, you will find both seem to return something with ArrayList as its runtime type, and on looking even harder, things get worse: There are two ArrayList classes . But all those problems will disappear completely if you declare your variables as List not ArrayList.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Biniman Idugboe wrote:
Also, what are the reasons for using  instead of

It's called programming to abstractions, not concretions otherwise known as the Dependency Inversion Principle (DIP)
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . programming to abstractions, not concretions . . .
Is that the same as, “Program to the interface, not the implementation”?
DIP
Isn't that the D in SOLID?

OP: if you search for SOLID you will find there are five principles which people follow in object‑oriented programming. Maybe the O is less important now than it was twenty years ago.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Junilu Lacar wrote:. . . programming to abstractions, not concretions . . .
Is that the same as, “Program to the interface, not the implementation”?

Yup, same thing. And yes, DIP is the D in SOLID.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!