• Post Reply Bookmark Topic Watch Topic
  • New Topic

General Question about Interfaces  RSS feed

 
Brian Moakley
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey All,

First off, hello. I'm new to the Java world. My team recently lost its only engineer, so we've been tasked to maintain our servlet code. Needless to say, I've been hitting the books.

I decided to get into the game by reading "Head First Java", and the following code snippet baffled me. There is no explanation, so I thought I'd post my question to the forums.

First off, here's a bit of the code:



The Nose array is killing me. I had learned that interfaces are great way of providing multiple inheritance. I've always assumed that interfaces were not meant to be instanced. In the above example, I see that the reference variable is creating an array of Noses. Is this just a bizzare way of referencing the Clowns class? The array is ultimately filled with Clown objects. Why would any one do this?

If you have the book, the example is on the last page of Chapter 8 (234). I'm just curious. Any perspectives on this issue is welcome.

Cheers!
Brian
 
P Lavti
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here the Nose array is getting instantiated, not the Nose object.
When you say Nose[]i = new Nose[3]; means you are creating an array of length 3 which will contain 3 Nose objects. Bus as soon as you'll

Compile will crib.

Another error in the code is that the class Clowns must be abstarct as it is not implementing the inherited method iMethod().
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"HomersGhost,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your display name here. Thank you for your prompt attention!

-Marc
 
Brian Moakley
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Name has been updated, per request.
 
Brian Moakley
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I abridged the above code for the sake brevity, but in order to illuminate my confusion, I'll provide the full source below.



I'm assuming that since all the objects are implementing the Nose interface, it is safe to put them in an array of Nose object types. My question -- since Nose can never be instanced, why would someone do this?

Again, thanks for the help.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because you can now iterate through the array, treating everything as a Nose, and calling iMethod() on each one.

The only other way to do this would be to make an array of Objects (note - capital 'O'). As you pull each one out, you'd have to figure out what it is, cast it back to its real type, and then call iMethod(). Every time a new class is written, you have to modify your check for the new type.

with an array of Nose-type objects, a year from now, somebody can give you an object you've never heard of, and the code will still work.
 
Ricky Clarkson
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you only have one implementation of an interface, then you don't need the interface; you can make the array be directly of the concrete type.

You lose some flexibility (but you can quickly regain it with a refactoring IDE) and lose some indirection. Losing indirection is good, if it wasn't buying you anything.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would agree with Fred Rosenberger. Do it the hard way to start with, and then you find changes are much easier. Set up the interface; with an IDE it will only take a few seconds to get all the methods to implement. NetBeans even puts "throw new UnsupportedOperationException();" as the text of all the methods, so you can't forget a method without finding out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!