SlideShare a Scribd company logo
When & Why
Interfaces, Abstracts, Traits
Interfaces: Primary Purposes
Interfaces allow you to define/create a common structure for your classes – to
set a standard for objects.
Interfaces solves the problem of single inheritance – they allow you to inject
‘qualities’ from multiple sources.
Interfaces provide a flexible base/root structure that you don’t get with
classes.
Interfaces are great when you have multiple coders working on a project – you
can set up a loose structure for programmers to follow and let handle details.
Interface: When to use
If you have a class that is never directly instantiated in your program, this is a
good candidate for an interface. In other words, if you are creating a class to
only serve as the parent to other classes, it should probably be made into an
interface.
When you know what methods a class should have but you are not sure
what the details will be.
When you want to quickly map out the basic structures of your classes to
serve as a template for others to follow – keeps the code-base predictable
and consistent.
Built-In Interfaces
Traversable
Iterator
IteratorAggregate
Throwable
ArrayAccess
Serializable
Closure
Generator
Abstract Class: When to use
You want to share code among several closely related classes.
You expect that classes that extend your abstract class have many
common methods or fields or require access modifiers other than public
(such as protected and private).
You want to declare non-static or non-final fields. This enables you to define
methods that can access and modify the state of the object to which they
belong.
Trait
Cannot type hint for trait
Can be used to fulfill and interface
Can include more then one
Consistency, DRY
Tip
Interface
Think “able”
May be implemented in different ways
Inheritance
Think “is-a”
Many things work the same way
Trait
Think “has-a”
Extra Patterns
Singletons
Factories
Collections
Dependency Injection
Alena Holligan
• Wife, and Mother of 3 young children
• PHP Teacher at Treehouse
• Portland PHP User Group Leader
• Cascadia PHP Conference (cascadiaphp.com)
@alenaholligan alena@holligan.us https://joind.in/talk/0cf82

More Related Content

PPTX
More oop in java
PPT
OOP for java
PPTX
Design Patterns - 01 Introduction and Decorator Pattern
PPTX
Design patterns
PPTX
Introduction to Software - Coder Forge - John Mulhall
PDF
Top 30 Technical interview questions
PDF
Web Design & Development - Session 9
PDF
Object Oriented Best Practices - Summary
More oop in java
OOP for java
Design Patterns - 01 Introduction and Decorator Pattern
Design patterns
Introduction to Software - Coder Forge - John Mulhall
Top 30 Technical interview questions
Web Design & Development - Session 9
Object Oriented Best Practices - Summary

Similar to When & Why: Interfaces, abstract classes, traits (20)

PPTX
Unit No 6 Design Patterns.pptx
PPTX
Let us understand design pattern
PDF
Basic design pattern interview questions
PPTX
SKILLWISE - OOPS CONCEPT
PDF
Six of the Best
PPTX
Design patterns Structural
PPTX
Oopsinphp
PPTX
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
PPT
Software Design Patterns
PPT
Software Design Patterns
PPTX
Java programming - solving problems with software
PPTX
JS Design patterns in Web technologies including oop techniques.pptx
PDF
Gang of Four in Java
KEY
Practical OOP In Java
PPTX
Template pattern
PPTX
java_inheritance_oop_20250730110153.pptx
PDF
Introduction to Java Interfaces: A Cornerstone of Advanced Java Programming
PPT
Design patterns
PDF
Review oop and ood
PPTX
Creativity vs Best Practices
Unit No 6 Design Patterns.pptx
Let us understand design pattern
Basic design pattern interview questions
SKILLWISE - OOPS CONCEPT
Six of the Best
Design patterns Structural
Oopsinphp
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Software Design Patterns
Software Design Patterns
Java programming - solving problems with software
JS Design patterns in Web technologies including oop techniques.pptx
Gang of Four in Java
Practical OOP In Java
Template pattern
java_inheritance_oop_20250730110153.pptx
Introduction to Java Interfaces: A Cornerstone of Advanced Java Programming
Design patterns
Review oop and ood
Creativity vs Best Practices
Ad

More from Alena Holligan (20)

PDF
2023 Longhorn PHP - Learn to Succeed .pdf
PDF
Environmental variables
PDF
Dev parent
PDF
Dependency Injection
PDF
Dependency Management
PDF
Experiential Project Design
PDF
Reduce Reuse Refactor
PDF
Organization Patterns: MVC
PDF
Object Features
PDF
Obect-Oriented Collaboration
PDF
WordCamp Portland 2018: PHP for WordPress
PDF
Let's Talk Scope
PDF
Demystifying Object-Oriented Programming #phpbnl18
PDF
Exploiting the Brain for Fun and Profit
PDF
Environmental Variables
PDF
Learn to succeed
PDF
Demystifying oop
PDF
Demystifying Object-Oriented Programming - PHP[tek] 2017
PDF
Demystifying Object-Oriented Programming - PHP UK Conference 2017
PDF
Exploiting the Brain for Fun & Profit #zendcon2016
2023 Longhorn PHP - Learn to Succeed .pdf
Environmental variables
Dev parent
Dependency Injection
Dependency Management
Experiential Project Design
Reduce Reuse Refactor
Organization Patterns: MVC
Object Features
Obect-Oriented Collaboration
WordCamp Portland 2018: PHP for WordPress
Let's Talk Scope
Demystifying Object-Oriented Programming #phpbnl18
Exploiting the Brain for Fun and Profit
Environmental Variables
Learn to succeed
Demystifying oop
Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Exploiting the Brain for Fun & Profit #zendcon2016
Ad

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Big Data Technologies - Introduction.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Unlocking AI with Model Context Protocol (MCP)
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Review of recent advances in non-invasive hemoglobin estimation
MIND Revenue Release Quarter 2 2025 Press Release
Diabetes mellitus diagnosis method based random forest with bat algorithm
Big Data Technologies - Introduction.pptx
MYSQL Presentation for SQL database connectivity
Chapter 3 Spatial Domain Image Processing.pdf
sap open course for s4hana steps from ECC to s4
Per capita expenditure prediction using model stacking based on satellite ima...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Unlocking AI with Model Context Protocol (MCP)

When & Why: Interfaces, abstract classes, traits

  • 1. When & Why Interfaces, Abstracts, Traits
  • 2. Interfaces: Primary Purposes Interfaces allow you to define/create a common structure for your classes – to set a standard for objects. Interfaces solves the problem of single inheritance – they allow you to inject ‘qualities’ from multiple sources. Interfaces provide a flexible base/root structure that you don’t get with classes. Interfaces are great when you have multiple coders working on a project – you can set up a loose structure for programmers to follow and let handle details.
  • 3. Interface: When to use If you have a class that is never directly instantiated in your program, this is a good candidate for an interface. In other words, if you are creating a class to only serve as the parent to other classes, it should probably be made into an interface. When you know what methods a class should have but you are not sure what the details will be. When you want to quickly map out the basic structures of your classes to serve as a template for others to follow – keeps the code-base predictable and consistent.
  • 5. Abstract Class: When to use You want to share code among several closely related classes. You expect that classes that extend your abstract class have many common methods or fields or require access modifiers other than public (such as protected and private). You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
  • 6. Trait Cannot type hint for trait Can be used to fulfill and interface Can include more then one Consistency, DRY
  • 7. Tip Interface Think “able” May be implemented in different ways Inheritance Think “is-a” Many things work the same way Trait Think “has-a”
  • 9. Alena Holligan • Wife, and Mother of 3 young children • PHP Teacher at Treehouse • Portland PHP User Group Leader • Cascadia PHP Conference (cascadiaphp.com) @alenaholligan alena@holligan.us https://joind.in/talk/0cf82