Constructing objects:
Once we have decided what kinds of objects we require in our system, we can describe them in general terms. Each category of objects we identify, such as 'bank account' or 'document', suggests a class of objects that share certain properties with one another.
A class is a definition of a category of objects. Having defined a class, we can construct objects using it, each object having its own state. Each category of objects in our system has a class definition.
A class defines:
- the items of data that make up the state of an object, including their type: for example, whether they are integers, strings, or types defined by other classes;
- the 'behaviour' of objects of the class, by which we mean the methods that can be invoked on objects of the class.
Usually we maintain a reference to an object. When we invoke a method or access an instance variable, it is this reference we use with a dot notation. So, there are usually three steps to using an object and these are as follows.
1 Define a class.
2 Construct an object.
3 Store an object reference.
Step 1: defining a User class
Classes are like templates or blueprints for objects. They do not make objects, but they describe the features that objects of a class will have when created. We will write the names of classes using a capital letter for each new word, following the convention used for Java's prede?ned classes.
By way of example, suppose that we have an applet and that we want to record how often users are accessing it. We want to define a class called User to model users of the applet. We wish to keep track of the user's identity (called userID) and email address (emailAddress), both of which can be represented as strings. We represent the number of times the applet has been visited as an integer, numOfAccesses. Below is a skeleton class de?nition for User, showing its instance variables:
public class User
{
private String userID;
private String emailAddress;
private int numOfAccesses;
// constructors have still to be written
// methods have still to be written
}
The keyword class introduces the name of the class. We call this first line of the class the class header.
Inside the brackets, we define any instance variables, methods and constructors. The instance variables and methods of a class or object are known collectively as its members.
Remember that Java has two kinds of type: primitives and reference types. A class de?nition creates a reference type (we will also say class type). So, the class above de?nes a new reference type called User. Java provides many predefined class types.
Step 2: constructing a User object
In order to create an object you use the facility known as construction, which uses the keyword new together with a class name. For example, given our class User, the code:
new User()
results in an object of type User being created. We call this invoking a constructor.
Step 3: storing a reference to a User object
We could declare a reference variable by giving its type followed by an identifier. This requires a simple statement of the form:
ClassName objectName;
where ClassName is the name of a class (either one we defined or one in a Java library, such as String) and objectName is an identifier.
Any such declaration creates a reference variable. In the example above, the type of the reference variable objectName is ClassName. To create a reference of type User, we would write something like: User john; In order to store a reference to an actual object in such a reference variable, we need to assign a value to the variable. This is usually done by assigning a newly constructed object as follows:
john = new User();
![1606_constructing object.png](https://www.expertsmind.com/CMSImages/1606_constructing%20object.png)
Figure shows the relationship between a reference variable and an object. Each object has its own copies of any instance variables.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Constructing objects homework and assignments? Live Constructing objects experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Constructing objects homework help, java assignment help and Constructing objects 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