3

It's a newbie question.

I understand the technical implementation of Interfaces, Abstract classes etc. and I do understand the programmatic differences and how and when to use them. I understand that an interface is like a forced contract without a concrete definition which all implementing classes must implement but I fail to understand why such a contract should be forced in the first place?

For instance, I've a hotel 'H' that has some rooms that can be reserved,

interface H{ //example method to implement reserveRoom(int n, ...) {} ... other methods } 

on the other hand I've travel booking sites A1, A2, A3 etc which might have their own discount/commission programs, which might up-sell/down-sell to their guests but what I don't get is what does it might have to do with Hotel 'H' booking policy. As the only thing Hotel H is interested in getting a room filled at their asking price.

Please explain why the need of such a design of using interfaces?

Thanks.

2 Answers 2

1

Consider interface like a rule-set for your implementations. If you follow the rule-set you can guarantee that consumers will be able to shift to your implementations seamlessly.

Just like JDBC interfaces for example. Java has provided set of interfaces as JDBC and the DB vendors do provide implementations for those interfaces.

That is why your statement like below, stands valid irrespective of which database you are connecting to.

Statement stmt = conn.createStatement( ) 

Consider, there were no interfaces (JDBC) and MySql driver changed the createStatement() to makeStatement(). You will have to change the code and you will end up writing Database specific code

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Amit, it definitely improved my understanding better than the hotel example in the textbook. Thanks for your input.
0

All forcing or mandatory checking the language does it to help catch our mistakes.

In this case it guards against the mistake of the booking site trying to reserve a room with a function you do not implement.

3 Comments

Thanks for your comment Erik. I guess what I wanted to know was whether there was a bigger design principle for which the concept of interface would be essential as avoiding mistakes could be avoided lets say using an IDE among other ways. Amit has commented a very beautiful answer below which provides the design principle behind interfaces.
Erik I think you are also right, probably it's the way Amit put it made the difference.
@avi the bigger design principle is to help in communication between different (often teams of) people. The 'thing' the multiple developers need to agree on (not democratically, they need to get the same understanding, usually it's one team deciding) is refined to the contract, aka interface, and all other details are somewhere else. Getting humans widely separated by space and time to co-operate on a de-facto shared project (such as integrating two booking systems) is very difficult and reducing the scope to the bare minimum is a tool to help in that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.