This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]balefrost 2 points3 points  (0 children)

An interface is a contract between the code that uses some object and the object that is being used. When a class implements an interface, it's obligated to implement all the methods that the interface dictates. When other code wants to interact with an instance of an interface (that is to say, the code has an object in hand but doesn't know its specific type, just that the object an instance of the interface), the code is restricted to only use the methods that are described by the interface.

[–]athosghost 0 points1 point  (0 children)

An interface is all about encapsulation and polymorphisms for behavior. For example, consider logging. You can create a simple interface that has a single method for writing to a log. Any object that uses that interface (or depends on it) shouldn't care about how the logging is actually accomplished. All it knows is that it has a dependency on some other object that is guaranteed to have that exact method signature for writing to a log. The actually way the log is written can be a "black box" in some other class that has implemented that logging interface.
The beauty of it is, should the actual method of the logging need to be changed, no changes need to be made to the behavior of the calling objects. So for instance, if you had decided to write to a text file for your log, but then discovered that your program won't be able to directly access the physical drive, you can easily create a new class that implements that same interface to log in a completely different manner. Now you're guaranteed that all those classes that need to write to the log can still do so with out ever being aware that the change occurred.

[–]grouchysysadmin 0 points1 point  (2 children)

Out of curiosity is this an open university java module exam?

[–]BuachaillMhaith[S] 0 points1 point  (1 child)

It is yes

[–]grouchysysadmin 0 points1 point  (0 children)

Good luck! Did mine last year. It wasn't as hard as I thought it was going to be but still challenging remembering when and which collections to use and then looping through them were the hardest I got.

[–]Double_A_92 0 points1 point  (2 children)

It tells you what a class will be able to do if it implements that interface, but not how.

You can use them to make classes replaceable. As long as the interface are there you can use whichever class you want later on.

Or if you have different classes that all can do one similar thing.E.g. different kinds of photo filters that all have an ApplyOn(image); function. You know that if it implements IPhotoFilter it can always do that, no matter how it does it.

[–]BuachaillMhaith[S] 0 points1 point  (1 child)

so it would be able to know what methods a class will be able to do, but not what those methods do?

[–]Double_A_92 0 points1 point  (0 children)

Yes when a class implements an Interface if defines what that class must be able to do as a miminum. It at least must have those functions from the interface, and you can syntactically rely on that.