Possible Duplicate:
The purpose of interfaces continued
I just started learning Java not so long ago.
I've come across Interfaces
which I know how to use but still can't quite grasp the idea of it.
As I understand it, interfaces
are usually implemented by classes, which then have to implement the methods declared in the interface.
The problem is - what exactly is the point? Wouldn't it be easier to just implement the methods from the interface as normal class methods? What exactly are the advantages of using interfaces?
I've tried looking for an answer on google. There were many but I still couldn't understand the point of it. I also read this question and its answers, but the whole contract thing just makes it more complicated...
Hopefully someone could simplify it good enough! :)
Thanks in advance!
An interface allows you to provide a different implementation at runtime, inject dependencies, separate concerns, use different implementations for testing.
Just think of an interface as a contract that a class guarantees to implement. The concrete class that implements the interface is irrelevant. Don't know if this helps.
Figured some code might help. This doesn't explain everything there is more to interfaces, keep reading but I hope this gets you started. Point here is that you can vary the implementation...
package stack.overflow.example;
public interface IExampleService {
void callExpensiveService();
}
public class TestService implements IExampleService {
@Override
public void callExpensiveService() {
// This is a mock service, we run this as many
// times as we like for free to test our software
}
}
public class ExpensiveService implements IExampleService {
@Override
public void callExpensiveService() {
// This performs some really expensive service,
// Ideally this will only happen in the field
// We'd rather test as much of our software for
// free if possible.
}
}
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// In a test program I might write
IExampleService testService = new TestService();
testService.callExpensiveService();
// Alternatively, in a real program I might write
IExampleService testService = new ExpensiveService();
testService.callExpensiveService();
// The difference above, is that we can vary the concrete
// class which is instantiated. In reality we can use a
// service locator, or use dependency injection to determine
// at runtime which class to implement.
// So in the above example my testing can be done for free, but
// real world users would still be charged. Point is that the interface
// provides a contract that we know will always be fulfilled, regardless
// of the implementation.
}
}
Interfaces can be used be used for many things. The most commons uses are polymorphism and dependency injection, where you can change the dependencies at run time. For example, suppose you have an Interface called Database which has one method called getField(...)
.
public interface Database
{
public void getField(String field);
}
Now suppose you have use two databases in your application depending on your client: MySQL and PostgreSQL. You will have two concrete classes:
public class MySQL implements Database
{
// mysql connection specific code
@Override
public void getField(String field)
{
// retrieve value from MySQL
}
}
And...
public class PostgreSQL implements Database
{
// postgre connection specific code
@Override
public String getField(String field)
{
// retrieve value from Postgre
}
}
Now depending on your client preference, you can instantiate a MySQL or PostgreSQL class in your main
public static void main(String args[])
{
if (args[2].equals("mysql"))
Database db = new MySQL();
else
Database db = new PostgreSQL();
}
//now get the field
String foo = db.getField();
Or you can use Factory/Abstract Factory or scripting engine with JS or Ruby.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With