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 method writeContent().
  • Add the Author, Commentator, and Viewer classes to implement the User interface.
  • Use the Writing trait in the Author class and implement its abstract method by making it return the string “Author, please start typing an article”.
  • Use the Writing trait in the Commentator class and implement its abstract method by making it return the string “Commentator, please start typing your comment”.
  • In the test() method, create Author and Commentator class objects and call the writeContent() 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.