Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make best use of Interfaces.

 
Atul Shukla
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Atul Shukla
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all these stuff.

-Atul
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic