Challenge: Static Methods & Properties and Traits
Create an interface and traits, and then use them in child classes.
We'll cover the following
Task
- Write an interface named
User
. - Write a trait named
Writing
that contains an abstract methodwriteContent()
. - Add the
Author
,Commentator
, andViewer
classes to implement theUser
interface. - Use the
Writing
trait in theAuthor
class and implement its abstract method by making it return the string“Author, please start typing an article”
. - Use the
Writing
trait in theCommentator
class and implement its abstract method by making it return the string“Commentator, please start typing your comment”
. - In the
test()
method, createAuthor
andCommentator
class objects and call thewriteContent()
method to return the following output.
Expected output
Author, please start typing an article and Commentator, please start typing your comment
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.