Encapsulation uses public, protected, and private. Inheritance shares behavior; interfaces define contracts without implementation.
Visibility
public— callable from anywhereprotected— class and subclassesprivate— declaring class only
Inheritance
class Admin extends User {
public function canPublish(): bool { return true; }
}
Single inheritance for classes; traits mix in horizontal reuse. Override methods with compatible signatures; use parent:: to call parent implementation.
Interfaces
interface Notifier {
public function send(string $message): void;
}
Classes implement interfaces; type-hint interfaces in constructors for testability (dependency injection).
Important interview questions and answers
- Q: Abstract class vs interface?
A: Abstract classes can hold state and partial implementation; interfaces are pure contracts—classes can implement many interfaces but extend one class. - Q: When use final?
A: Prevent overriding a method or extending a class when invariants must not change.
Self-check
- Can a class implement two interfaces?
- Which visibility should internal helpers use?