This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Question On Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question On Interface" Watch "Question On Interface" New topic
Author

Question On Interface

Marianne Rivera
Greenhorn

Joined: May 05, 2009
Posts: 5
Hello,

I am reading head first java book, and I see this statement on page 229

"An interface is like a 100% pure abstract class. It defines only abstract methods."

so I was thinking that if I define an instance variable, the compiler will tell me that there is an error.



but the compiler has no error. Does this mean that if I implement this interface, I automatically inherit also
the intSample?

I was just trying to expirement but I really dont know as of the moment if there is some benefit of doing this.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10269
    
    8

Marianne Rivera wrote: Does this mean that if I implement this interface, I automatically inherit also
the intSample?



Yes you are correct.
Remember, interfaces are not always used as "templates". If you want to define some constants, usually, interfaces are the way to go. Check out SwingConstants for example. It is an interface, and defines many constants used in the swing UI.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575



Compile it and open in the decompiler
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Seetharaman is right.

Every method declared in an interface is automatically public and abstract, and every field declared is automatically public, static and final.

Maneesh Godbole wrote:If you want to define some constants, usually, interfaces are the way to go.

Ugh, please no.

Joshua Bloch explains why constants interfaces are wrong in Item 19 of his Effective Java book (Item 17 in the first edition). If you want a single place for all your constants, a final utility class should be used. Basically exactly what is described in the book, but the class should also be final (you can't subclass it because the constructor is private; making the class final makes sense).

Sun have used this technique themselves as well, with java.sql.Types (although again, only implicitly final...)


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks for the information Rob
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

+1 on not using interfaces to define constants, and the Bloch reference explains why.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question On Interface