File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How to make best use of Interfaces. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to make best use of Interfaces." Watch "How to make best use of Interfaces." New topic

How to make best use of Interfaces.

Atul Shukla
Ranch Hand

Joined: Oct 09, 2006
Posts: 34
I would like to know, what is the best way to make full use of Interfaces, don't tell me that they support multiple Inheritance, i want to know how should i use them in Object oriented programming and why.

<a href=>check this thread too </a>

Thanks in advance!!

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Although interfaces are a part of the SCJP objectives this topic belongs in a more general forum. Moving to Java In General (Intermediate)...
[ October 17, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Quite simply, interfaces should be used whenever two classes share a commonality, but the commonality does not suffice the 'is-a special type of' commonality expressed through inheritance.

Whever there is commonality, like a bird and a boat delivering mail, you should recognize that commonality, and have those classes implement a common interface. "factoring out interfaces" is a common activity when having completed a class diagram of a problem domain.


-Cameron McKenzie
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 356

when you apply the principle of programming to an interface, not an implementation, in Java, you will use interfaces for most non-primitive types in your program. They can occur as a type in a declaration, as a method parameter type or a return type.

Here are some examples:

Whatever the context of these two lines is, in most cases it would be best if Dog, Owner and Possession were interfaces.

If for example Possession were not an interface but a class, then the public method would depend on and be bound to a certain implementation. This is a problem in itself already which can have very concrete disadvantages. It would also be a symptom which often indicates that the program is not really object oriented, which means that it does not base its primary logic on encapsulation, inheritance and polymorphism.

The concrete damage is a lot less severe for APIs which are not published to anyone, not even to a single co-worker. If class types are used often, it still is a symptom for severe design problems.

I wrote a lot about this very point in my Design Principles article. The sections "Consequences" and "Common justifications for not using an interface type" elaborate more on this.

[ October 17, 2006: Message edited by: Kai Witte ]

Kai Witte's business website Kai Witte's private homepage
Atul Shukla
Ranch Hand

Joined: Oct 09, 2006
Posts: 34
Thank you for all these stuff.

I agree. Here's the link:
subject: How to make best use of Interfaces.
It's not a secret anymore!