File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Object references as of type Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object references as of type Interface" Watch "Object references as of type Interface" New topic

Object references as of type Interface

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629

My friend always says that it's a good Java practice to have Object references in Java to be of an interface type(Left hand side should always be an interface) . I do not understand why tho. Can someone please explain?

[ May 24, 2008: Message edited by: Arjun Reddy ]

Be Humble... Be Nice.
Luciano Assis

Joined: Apr 30, 2008
Posts: 16

Using interfaces is a good practice because you can use the polymorphism.

For example.. you can make a method with interfaces parameters, so you will be able to call this method passing any object that implements that interface.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

imagine you and your friend are both writing code for a project. you have a call like:

Foo myFoo = FriendsClass.Bar();

Bar is a static method that your friend is writing in his code to return some object. if Foo is a class, then your friend is locked into returning that class, forever.

however, if Foo is an interface, your friend can return ANY class that suits his needs, as long as it implements your Foo interface. It frees him up to do whatever he wants or needs, or to refactor his code a year from now to make it better, and you don't have to touch yours.

[ May 25, 2008: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Foo myFoo = new Bar();

Bar is a static method that your friend is writing in his code to return some object.

I think fred meant:

Foo myFoo = Bar();

or better yet

Foo myFoo =;

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Rob Spoor

Joined: Oct 27, 2005
Posts: 20275

Let me give you some highly used examples:

All three examples declare the variable with an interface type - List, Map and Collection. The reason for this is, mostly, because it helps you quite a lot if you ever need to change the implementation.

For instance, all of a sudden you find that LinkedList is better for you than ArrayList. If you had declared the list type as ArrayList, you would potentially have to change many lines of code. Furthermore, you ran the risk of calling methods only available in ArrayList. The change to LinkedList will be so much easier (just one line) if you declare the reference as List.
The same holds for the other two.

The general rule is, declare variables as wide as possible. List instead of ArrayList, Component instead of JComboBox, Number instead of Integer.
Only when you need functionality that is specific to a more specific type you will need to use that type in the declaration. For instance if you really need a JComboBox, and not just any Component.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Object references as of type Interface
It's not a secret anymore!