aspose file tools*
The moose likes Java in General and the fly likes Coding to an interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Coding to an interface" Watch "Coding to an interface" New topic
Author

Coding to an interface

Gaurav Arora
Ranch Hand

Joined: Aug 13, 2007
Posts: 35
I have read about coding to an interface time and again. I also understand that the practice builds flexibility in your code and flexibility is never a bad thing, well almost never. All in all, it's a good practice and I agree with it.

My question is, does it help (or hurt) performance of an application in any way?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Gaurav Arora:
flexibility is never a bad thing


I'm not sure I agree with this.

Flexibility is good if it helps with current requirements, or genuinely likely future requirements. Flexibility is also good, if it can be achieved without additional complication.

Flexibility that is not needed now, and has only speculative use in the future, but which adds complication, is bad. It costs money to implement. It costs money to debug the extra complexity. It costs money to maintain the more-complex code.

An example of bad flexibility I've often encountered is over-generalisation. An interface that could have been simple has been made complex to understand and to implement, for the sake of generality for which there was no need at the time, and for which no need has arisen in the years afterwards.

Remember, YAGNI.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Normally it doesn't. You still have the same objects with the same methods and fields. The code doesn't change (much) if you declare something as List or as ArrayList.

Of course, when you want to perform specific code for certain implementations, then you will need to a) check if the object is an instance of the class, and b) cast. This will hurt performance - albeit only slightly. You won't even notice the difference.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

Originally posted by Rob Prime:
Normally it doesn't. You still have the same objects with the same methods and fields. The code doesn't change (much) if you declare something as List or as ArrayList.

Of course, when you want to perform specific code for certain implementations, then you will need to a) check if the object is an instance of the class, and b) cast. This will hurt performance - albeit only slightly. You won't even notice the difference.

Note that if you find that you need to use 'instanceof' or casts, it's a sign that the design of your software might have a flaw somewhere. In a well-designed piece of software, you shouldn't need to use 'instanceof' or casts.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Coding to an interface