Pages

Friday, June 22, 2012

Difference between Abstract class and Interface

PHP 

Abstract Class Interface
Abstract methods doesn’t have any implementation. Any class that contains at least one abstract method must also be abstract Interface methods by default are abstract methods only. So one cannot declare variables or concrete methods in interfaces.
The Abstract methods can declare with Access modifiers like public, internal, protected. ' When implementing in subclass these methods must be defined with the same (or a less restricted) visibility. All methods must be public.
Abstract class can contain variables and concrete methods. Interfaces cannot contain variables and concrete methods except constants.
A class can Inherit only one Abstract class. Multiple inheritance is not possible for Abstract class. A class can implement many interfaces. Multiple interface inheritance is possible.

 


.NET
Feature
Abstract class
Interface
Multiple inheritance A class may inherit only one abstract class. A class may inherit several interfaces.
Can Provide Code Can provide complete, default code just the details that have to be overridden. Cannot provide any code, just the signature.
Access Modfiers Any access modifiers for the methods or properties Public methods only
Homogeneity Use for common behaviour Use only for shared method signatures.
Fields and Constants An abstract class can have fields and constrants defined No fields can be defined in interfaces



No comments:

Post a Comment