PHP, OOP interface and abstraction

Possible Duplicate:
What is the difference between an interface and abstract class?

I read a guide about the difference between interface and abstraction but i didnt understand it the guide say's: why using exactly interface when we can use an abstract method, the answer is that the using of interface not require us to inherit from certain abstract class, Thus two classes that not inherit from the same class can contain similar interface, Actually using interface allowing us not inflate the parent class with redundant methods. i was really tying to understand it but it did not register, if some one can please help me here i will be very thankful.


Some recommendations on when to use an interface and an abstract class (courtesy: MSDN)

1) If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

2)If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

3) If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

4) If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

http://msdn.microsoft.com/en-us/library/scsyfw1d%28v=vs.71%29.aspx


An interface allows you to have polymorphism based on similar collections of methods across several unrelated class hierarchies. This means that you can code a method to use any one of a whole range of classes which are not necessarily related to each other. You can also mix and match by having more than one interface applied to a class, so the class can be used for many things.

By contrast, abstract methods only allow you to use direct descendants of the parent class interchangeably, which can be limiting as you can't then have more than one collection of behaviours (because classes can only have one parent).

链接地址: http://www.djcxy.com/p/54256.html

上一篇: 什么是在C#中的行为抽象类和接口?

下一篇: PHP,OOP界面和抽象