Extending the User class:
As an example, let us say that we have developed a number of applications using our User class, but that we now want to differentiate between normal users and 'privileged' users. The former will be allowed to access some of the facilities of the application; the latter will be able to access all the facilities of the application but will need to pay for the privilege. Privileged users have all of the attributes of normal users, and some additional ones.
Since privileged users will still require the instance variables and methods associated with ordinary users, we can develop a new class called PrivilegedUser that inherits from User. We can continue to use the User class alongside the PrivilegedUser class. In order to cater for the privileged users we will need to provide them with a password, to prevent other users accessing their facilities.
public class PrivilegedUser extends User
{
// additional instance variables
// for example, a string password
private String password;
// methods for a privileged user
// for example, a method that
// changes a privileged user's password.
public void setPassword ()
{
// some code that sets password
}
}
If we have developed the User class following the principles of information hiding, we will not need to know the details of how User achieves its work; we can simply get on with writing PrivilegedUser. The relationship between the two classes allows us to say that an object of the PrivilegedUser class is-a User object. By this, we mean that any methods we can invoke on a User object can be invoked on a PrivilegedUser object, subject to the access modifiers of the class allowing it. For example, a PrivilegedUser object can make use of the User method getEmailAddress because that method is inherited. We draw such a relationship as illustrated in Figure
![547_Extending the.png](https://www.expertsmind.com/CMSImages/547_Extending%20the.png)
You may think of a PrivilegedUser object as a User object, depending on which is more convenient. On the other hand, a User object cannot do the work of a PrivilegedUser object, because a User object does not have all the instance variables or methods that a PrivilegedUser has. If you keep in mind this relationship between a subclass and its superclass you will be able to understand when casting and assignment between reference types are legal., with the inheriting class below its superclass and connected to its superclass with an open-headed arrow.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Extending the User class homework and assignments? Live Extending the User class experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Extending the User class homework help, java assignment help and Extending the User class projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours