Skip to main content

Clarification on interfaces

1 reply [Last post]
wiredvs
Offline
Joined: 2011-01-24

Hello,
I am new to Java and have some questions regarding 'interfaces' and how they are used. I understand why they are used (the example in the Java Tutorials describing a proprietary program used as a navigation system, and manufacturers like GM, Toyota etc... using an interface to implement it in their own way was very descriptive).
What I don't understand is why they are used as opposed to just leaving a text file with your software describing the methods your class uses and how to call them, as well as any constants they use. My understanding is that interfaces act as a 'contract' or 'blueprint' for a class to implement your methods. But I know there's more to it than that that I'm missing.
Can I, for example write a class with twelve methods, then write two or three different interfaces to process the data in different ways? So, if wrote a class to calculate sales data for a store; could I write my class like the example above and write one interface for say 'weekdays', one for 'weekends', and one for 'holidays', substituting different constants and methods types so I can compensate for the fluctuation in customers?
It seems however, that I could write my class this way anyway, with specific methods to call for 'weekends', 'weekdays', and 'holidays' and leave out interfaces entirely.
So I am wondering if someone could shed some more light on interaces and their use...or at least point me in the right direction to continue my own research. I did search around this forum and couldn't find any references that answered my question.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cae_jones88
Offline
Joined: 2011-01-04

Interfaces, the way I've come to understand them, allow you to reference classes that function similarly-but-differently.

For instance, I have several games that I've made, and made a program that enables the user to load any one of them in the same window. This works because all of the games ipmlement the Game interface; it'd be impractical to make them instances of the same class, but as the same interface, my loader can handle all of them.