Abstract classes, traits and interfaces Flashcards
Learn what abstract classes, traits and interfaces are, the differences between them, learn when to use each of them, and what the best practices are.
What is an abstract class?
An abstract class is a class that cannot be instantiated and is meant to serve as a blueprint for other classes to inherit from. It contains abstract methods which are declared but not implemented within the abstract class itself. These methods must be implemented by any class that extends the abstract class.
When are abstract classes useful?
Abstract classes are useful whenever you want to define a common set of methods and properties that multiple classes will share. By using an abstract class, you can ensure that these methods and properties are implemented consistently across all the subclasses.
What is an interface?
An interface in PHP is a contract that defines a set of methods that a class must implement. It specifies the method signature without providing implementation details. An interface can be seen as a blueprint for classes to follow.
When are interfaces useful?
Interfaces are useful when you want to ensure that multiple classes adhere to a specific set of methods. They provide a way to enforce a common code structure across different classes, enabling polymorphism and code reusabilty.
What is a trait?
A trait is a mechanism for code reuse that allows developers to reuse methods and properties in multiple classes. Traits are similar to abstract classes and interfaces but they allow for horizontal code reuse instead of vertical inheritance.
When are traits useful?
Traits are useful whenever you want to share functionality across different classes that are not necessarily related by inheritance. They provide a way to mix and match behaviour in a flexible manner.
What are the differences in how abstract classes, interfaces and traits handle inheritance and implementation?
- Abstract classes provide a way to define common methods and properties that subclasses will inherit.
- Interfaces define a contract that classes must adhere to by implementing specific methods.
- Traits allow for code reuse across unrelated classes by mixing in additional methods and properties.
What access modifiers can abstract classes have?
Abstract classes can have methods and properties with different access modifiers (public, protected, private).
What access modifiers can interfaces have?
Interfaces can only have methods with public access.
What access modifiers can traits have?
Traits can have methods and properties with any access modifier.
How do abstract classes, interfaces and traits differ when it comes to multiple inheritance?
- Abstract classes can be used for single inheritance where a subclass can inherit from one abstract class.
- Interfaces can be used for multiple inheritance, where a class can implement multiple interfaces.
- Traits can be used for horizontal code reuse, allowing a class to mix in multiple traits.
How do abstract classes, interfaces and traits differ on method implementation?
- Abstract classes can have both abstract methods (not implemented) and concrete methods (implemented).
- Interfaces only have method signatures without implementation details.
- Traits can have fully implemented methods that can be used across multiple classes.
How do abstract classes, interfaces and traits differ with instantation?
- Abstract methods cannot be instantiated directly but can be used as a base for creating concrete classes.
- Interfaces cannot be instantiated directly and can only be implemented by classes.
- Traits cannot be instantiated directly but can be used in classes.
When should you use abstract classes?
You should you abstract classes when you want to define a default structure for subclasses to inherit from and to define default implementations for methods and properties.
What should you avoid when creating abstract classes?
You should avoid creating abstract classes that are too large or have too many responsibilities.
When should you use interfaces?
You should use interfaces when you want to enforce a specific set of methods that a class must implement and to achieve polymorphism and code reusability.
What should you avoid when creating interfaces?
Avoid creating interfaces with too many methods, this can lead to a lack of cohesion.
When should you use traits?
You should use traits when you want to share behaviour across unrelated classes and to avoid code duplication and promote code reuse.
What should you avoid when creating traits?
You should avoid creating traits that are too complex or have too many dependencies.
Can a class implement multiple interfaces in PHP?
Yes in PHP classes can implement multiple interfaces which enables more flexible design patterns.
Can you use both an abstract class and a trait in the same class?
Yes you can extend an abstract class and use a trait in the same class, combining both vertical inheritance and horizontal code reuse.
When should you use traits instead of interfaces?
You should use traits when you want to share behaviour (methods) across multiple classes, whereas interfaces should be used to enforce a contract of methods that must be implemented.