Two Laptop Bag*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
Author

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=http://www.coderanch.com/t/405075/java/java/Reusabilty-code-lack-interfaces-polymorphism>check this thread too </a>

Thanks in advance!!

-Atul
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
    
    1

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.

Cheers!

-Cameron McKenzie
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 356
hello,

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.

Kai
[ 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.

-Atul
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make best use of Interfaces.
 
Similar Threads
Any Suggestion about a small project?
HTML Parser for Java
completely lost in terms of trying to understand Collections.synchronizedList
Any good resources about design of XML
Display a JSP include in a textarea