• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface on LHS of expression  RSS feed

 
Mark Knutsen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading through Head First Java, 2nd ed, and I got a number of the questions on page 576 wrong because I assumed code like this wouldn't compile (assuming it's within a legal class and method):


...and yet it does. I thought java.util.List is an interface, not a class, and therefore is only valid in the context of "implements...", not on the left-hand side of an expression like this. So what makes this different from the following?


Earlier in the book it says, "A Java interface is like a 100% pure abstract class," but this example seems to show that in this case, the interface is treated exactly like an abstract class. Is that right?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, this is not an expression. It is a declaration. That is, this statement declares a variable that has type "List<Animal>" and is named list.

Second of all, you said "an interface...is only valid in the context of "implements...". This is not true. You can also use interfaces as the type for a varable declaration, a parameter declaration, or a return type. In fact, this is where interfaces show their power. In this example, you are creating a concrete object from the ArrayList<Animal> class. Since ArrayList implements the List interface, you can assign this object to a reference of type List<Animal>. (Note: there are some further complications due to generics, but I'm ignoring those for now.) From this point on, the code will only be able to use the "list" variable as a List, not as an ArrayList. But this is a Good Thing! One of the benefits is that you can later decide to change the concrete object to a Vector or LinkedList. This change will only affect a single line of code (e.g. the one you have shown here). You don't need to go through the rest of the code to make any changes since it does not depend on the specific implementation; it only depends on the List interface.

I hope this helps. If you want to explore this further, you should continue learning about inheritence, polymorphism, and reference variables. Inheritence and polymorphism are a big part of Object Oriented Programming.

Layne
 
Mark Knutsen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:
First of all, this is not an expression. It is a declaration. That is, this statement declares a variable that has type "List<Animal>" and is named list.

Of course; sorry. I should have said "statement" and left it at that

Second of all, you said "an interface...is only valid in the context of "implements...". This is not true. You can also use interfaces as the type for a varable declaration, a parameter declaration, or a return type.

So this declaration is taking advantage of List's position in the class hierarchy to define a more-generic variable, aside from any distinctions between interfaces and classes, yes?

[ September 16, 2005: Message edited by: Mark Knutsen ]
[ September 16, 2005: Message edited by: Mark Knutsen ]
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List<Animal> list = new ArrayList<Animal>();

Java interfaces and Java classes are both Java types. I would venture to say that you understand the distinction between a class and an interface so let's put that aside and talk about types.

Java allows you to define your own types. You can inherit a new type from an existing type. You can inherit a new type from several existing types at the same time. A type that inherits from another types is at least one of those types.

Since an ArrayList inherits from List, an ArrayList is-a List. Therefore there is no problem in assigning an ArrayList to a List. In this case you have an Animal List being assigned an Animal ArrayList. Same deal.

Now, if you used an ArrayList instead it would work but it's less flexible. Consider this:

void foo( ArrayList list ) { ... }

The caller will have no choice but to use an ArrayList or a derived ArrayList when calling method foo. If you replace that with type List you have far greater flexibility in terms of what objects can use you method.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Knutsen:

So this declaration is taking advantage of List's position in the class hierarchy to define a more-generic variable, aside from any distinctions between interfaces and classes, yes?

[ September 16, 2005: Message edited by: Mark Knutsen ]

[ September 16, 2005: Message edited by: Mark Knutsen ]


Yes. This is infact a very powerful use of interfaces as both Rick and I described earlier.

Layne
 
Mark Knutsen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for the very lucid, extremely quick help!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!