Class Basics and Benefits

Screen Shot 2018-12-19 at 1.57.19 PM

In Java, classes are composed of data and operations on the data.

Ultimately, there only exist data and operations, but some data and operations can become a guiding template for objects. Objects are data and operations that follow the guide. Think of the class as a generic description, and an object as a specific item of that class.

For example, a Griffith class might have the following data: name, age, and allegiance. All Griffiths have these three data items, and that’s what makes it a Griffith class. We can create an object of the Griffith class by specifying an identifier for the object (for example, griffith1) along with a name, age, and allegiance for a particular Griffith (for example, Femto, 24 , God Hand). The identifier of the object is called the object reference.

Creating an object of a class is called instantiating an object, and the object is called an instance of the class. Many objects can be instantiated from one class. There can be many instances of the Griffith class, that is, many Griffith objects can be instantiated from the Griffith class. For example, we could create a second object of the Griffith class, griffith2, with its data as White Falcon, 24, Band of the Falcon.

The data associated with an object of a class are called instance variables, or fields, and can be variables and constants of any primitive data type (byte, short, int, long, float, double, char, and boolean), or they can be objects of a class.

The operations for a class, called methods, set the values of the data, retrieve the current values of the data, and perform other class-related functions on the data. For example, the Griffith class would provide methods to set the values of the name, age, and allegiance; retrieve the current values of the name, age, and allegiance; and perhaps promote a Griffith to another age.

Invoking a method on an object is called calling the method. With a few exceptions, only class methods can directly access or change the instance variables of an object. Other objects must call the methods to set or retrieve the values of the instance variables. Together, the fields and methods of a class are called its members.

In essence, a class is a new data type, which is created by combining items of Java primitive data types and objects of classes. Just as the primitive data types can be manipulated using arithmetic operators (+, −, *, /, and %), objects can be manipulated by calling class methods.

We like to think of classes as similar to dragons: a protective outer armor to encase pulpy organs. Because the methods to operate on the data are included in the class, they provide a protective coating around the data inside. In a well-designed class, only the class methods can change the data. Methods of other classes cannot directly access the data. We say that the data are private to the class.

People higher in autism or psychopathy are well-designed classes in this sense. Entheogens allow class methods from elsewhere to change the data. – Speaking of which, psilocybin might be legalized in Oregon. Keep an eye on that because I’m willing to take that market share.

Sorry, I get distracted, what I was saying was that the class encapsulates the data, and the methods provide the only interface for setting or changing the data values. The benefit from this encapsulation is that the class methods ensure that only valid values are assigned to an object. For example, a method to set Griffith’s allegiance may not accept the value Argonauts.

Let’s look at another example of a class. The Arachnophobia class, has the instance variables signs, reasons, and treatment. An object of this class, arachnophobia1, could be instantiated with data values of uneasy, instinctive, and systematic desensitization. Another object of the Arachnophobia class, arachnophobia2, might be instantiated with the values panic attack, cultural, relaxation techniques. Methods of the Arachnophobia class ensure that only valid values are set for the signs, reasons, and treatment.

For example, the class methods may not allow us to set a date with a value of “contentment”, “magic”, and “prayer”. If we were dealing with dates, other class methods may increment the date to the next day and provide the date in mm/dd/yyyy format.

Notice that the class names we used, Griffith and Arachnophobia, begin with a capital letter, and the object names, griffith1, griffith2, and arachnophobia1, start with a lowercase letter.

By convention, class names start with a capital letter. Object names, instance variables, and method names conventionally start with a lowercase letter. Internal words start with a capital letter in class names, object names, variables, and methods. There are many benefits to using classes in a program. Some of the most important benefits include reusability (not only in the current program but also in other programs), encapsulation, and reliability. A well-written class can be reused in many programs. For example, an Arachnophobia class in the brain could be used in a film program, a survival program, a science program, and many more applications that rely on arachnophobias.

Reusing code is much faster than writing and testing new code. As an added bonus, reusing a tested and debugged class in another program makes the program more reliable. Encapsulation of a class’s data and methods helps to isolate operations on the data. This makes it easier to track the source of a bug. For example, when a bug is discovered in an object of the Griffith class, then you know to look for the problem in the methods of the Griffith class, because no other code in your program can directly change the data in a Griffith object.

This is why Mind partitioned itself into Name classes. It makes it easier to directly control   where we are going.

You do not need to know the implementation details of a class in order to use it in your program. Does a Date class store the date in memory as three integers, month, day, and year? Or is the date stored as the number of milliseconds since 1980? As with the unknowable binding mechanism causing us, the beauty of object orientation is that we don’t need to know the implementation of the class; all we need to know is the class application programming interface (API), that is, how to instantiate objects and how to call the class methods.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s