Reputation: 1213
what is the difference between below two design principles?
these two principle saying the same thing but in two different ways.
Upvotes: 1
Views: 172
Reputation: 176
In the object-oriented world, these terms used frequently and interchangeably. Interface is nothing but abstraction and implementation is nothing but concrete.
Upvotes: 0
Reputation: 25694
They are essentally saying the same thing in different words.
You should write your class so that it depends on an abstract idea (like an interface) instead of a concrete implementation of an idea. This allows you to change behavior in pieces instead of having to re-write whole chunks of code.
See Dependancy Injection.
Example:
public class Chef : IResturauntWorker
{
// This is an example of writing to an interface instead of an
// an implementation. Because the Chef class implements the
// IResturauntWorker interface, the chef can be swapped in with other
// resturaunt workers like bussers or waiters.
public void Chop(Carrot c)
{
// code to chop a carrot
// this is an example of depending on an implementation
// because Carrot is a concrete class, this method can
// only be used with a Carrot
}
public void Chop(IVegetable c)
{
// code to chop a Vegetable
// this is an example of depending on an abstraction
// because IVegetable is a abstraction of an idea
// (a vegetable, in this case), this method can be
// used with any class that implements IVegetable,
// including carrots, celery, onions, etc.
}
}
Upvotes: 2
Reputation: 2209
Interface is nothing but providing mean to communicate diffrent types of implementation Abstraction nothing but creating generic class with some abstract method.
Upvotes: 0
Reputation: 3604
Interface is an abstraction of a concrete class, so 2. is a subset of 1. The principle 1. has wider applicability (you can use it for any kind of interface, not only those used in object-oriented programming).
Upvotes: 6