Challenge: Inheritance
Try to implement the concept of inheritance.
We'll cover the following
Task
- Create a
User
class. Add aprotected
property named$username
to the class and create a setter method that can set the value of the$username
. - Create a class named
Admin
that inherits theUser
class. - Add a
public
method namedexpressYourRole()
to theAdmin
class and make it return the class name. - Add another
public
method namedsayHello()
to theAdmin
class. This method should return the stringHello admin, XYZ
with the$username
instead ofXYZ
. - Inside the
test()
method, create an object$admin1
of the classAdmin
, set its name toBalthazar
, and say hello to the user.
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.