So lets say I have this interface:
public interface IBox
{
public void setSize(int size);
public int getSize();
public int getArea();
//...and so on
}
And I have a class that implements it:
public class Rectangle implements IBox
{
private int size;
//Methods here
}
If I wanted to use the interface IBox, i can't actually create an instance of it, in the way:
public static void main(String args[])
{
Ibox myBox=new Ibox();
}
right? So I'd actually have to do this:
public static void main(String args[])
{
Rectangle myBox=new Rectangle();
}
If that's true, then the only purpose of interfaces is to make sure that the class which implements an interface has got the correct methods in it as described by an interface? Or is there any other use of interfaces?
Don't forget that at a later date you can take an existing class, and make it implement
IBox
, and it will then become available to all your box-aware code.This becomes a bit clearer if interfaces are named -able. e.g.
etc. (Naming schemes don't always work e.g. I'm not sure
Boxable
is appropriate here)The purpose of interfaces is polymorphism, a.k.a. type substitution. For example, given the following method:
When calling the
scale
method, you can provide any value that is of a type that implements theIBox
interface. In other words, ifRectangle
andSquare
both implementIBox
, you can provide either aRectangle
or aSquare
wherever anIBox
is expected.Here is my understanding of interface advantage. Correct me if I am wrong. Imagine we are developing OS and other team is developing the drivers for some devices. So we have developed an interface StorageDevice. We have two implementations of it (FDD and HDD) provided by other developers team.
Then we have a OperatingSystem class which can call interface methods such as saveData by just passing an instance of class implemented the StorageDevice interface.
The advantage here is that we don't care about the implementation of the interface. The other team will do the job by implementing the StorageDevice interface.
Interfaces are a way to make your code more flexible. What you do is this:
Then, later, if you decide you want to use a different kind of box (maybe there's another library, with a better kind of box), you switch your code to:
Once you get used to it, you'll find it's a great (actually essential) way to work.
Another reason is, for example, if you want to create a list of boxes and perform some operation on each one, but you want the list to contain different kinds of boxes. On each box you could do:
(assuming IBox has a close() method) even though the actual class of myBox changes depending on which box you're at in the iteration.
One of the many uses I have read is where its difficult without multiple-inheritance-using-interfaces in Java :
Now, Imagine a case where:
but,
Better design would be:
Animal does not have the chew() method and instead is put in an interface as :
and have Reptile class implement this and not Birds (since Birds cannot chew) :
and incase of Birds simply: