Challenge: Abstract Classes and Methods
Try to create an abstract class and make the child classes inherit from that abstract class.
We'll cover the following
Task
- Create an abstract class named
User
with an abstract method namedstateYourRole()
. - Add a
protected
property named$username
to theUser
abstract class. Then, add thepublic
setter and getter methods to set and get the$username
. - Create an
Admin
class that inherits the abstractUser
class. Define thestateYourRole()
method in the child class and make it return the stringadmin
. - Create another class named
Viewer
that inherits theUser
abstract class. Define the methods that should be defined in each child class of theUser
class. - Inside the
test()
method, create an object from theAdmin
class. Set the$username
toBalthazar
and make it return the stringadmin
by calling the appropriate method.
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.