Please see my other Object-Oriented Programming (OOP) articles.
Knowing How to Leverage Abstractions
Topic | Abstract classes | Interfaces |
---|---|---|
Implementation details: | Some members (methods). | No. |
Fields: | Yes | No. |
Inherit from: | Abstract class, interface | Interface only. |
Members can have access modifiers: | Yes. Abstract members private by default. |
No. Interface members public by default. |
Implementation Details: Abstract Classes
As illustrated in the next example, the abstract Employee class may define a constructor method for initializing some of its data.
As specified in the table above, no implementation details are allowed within interfaces.
Field: Abstract Classes
As illustrated in the previous example, the abstract Employee class may contain some field for data storage.
As specified in the table above, no fields are allowed within interfaces.
Inherit From
Abstract classes may inherit other abstract classes or interfaces whereas an interface may only inherit another interface.
Members with Access Modifiers
As illustrated in the previous example, the abstract Employee class may use access modifiers (default: private), but interfaces may not (default: public).