Challenge: Interfaces
Write a class and make it implement multiple interfaces along with inheritance.
We'll cover the following
Task
- Create a
User
class with a protected$username
property and methods that can set and get the$username
. - Create an
Author
interface with two abstract methods that can give the user an array of authorship privileges. The first method issetAuthorPrivileges()
with the parameter$array
, and the second method isgetAuthorPrivileges()
. - Create an
Editor
interface with the methodsetEditorPrivileges()
with the parameter$array
, and another with thegetEditorPrivileges()
method to set and get the editor’s privileges. - Create an
AuthorEditor
class that extends theUser
class and implements both theAuthor
and theEditor
interfaces. - Add some methods in the
AuthorEditor
class that it should implement and the properties that these methods force us to add to the class. - In the
test()
method, create an object named$user1
from the classAuthorEditor
and set its$username
toBalthazar
. Set an array of authorship privileges, includingwrite text
andadd punctuation
, in the$user1
object. Set the editorial privilegesedit text
andedit punctuation
in the$user1
object. - Write code in the
test()
method to get and print the$user1
name and privileges.
Expected output
Balthazar has the following privileges: write text, add punctuation, edit text, edit punctuation,
Coding exercise
Since these problems are designed for your practice, try to solve them yourself first. If you get stuck, you can click the “Show Solution” button to see how the problem can be solved. Good luck!
Get hands-on with 1400+ tech skills courses.