The fundamentals of Java

Being an object-oriented programming (OOP) language, Java has a substratum of classes and objects set as its foundation. This blog will briefly describe these two concepts.

(Courtesy iDelsoft)

Classes

In order to describe a real world object in our program, we start with a generalized version of it, a version with all of the attributes and behavior of the thing, but without any specific values. In Java, we call this uninitialized object a class.

You can think of a class as a blueprint. For instance, when planning the construction of a neighborhood, a contractor will design a handful of designs for the different houses. Those blueprints all have the same general shape and layout, but, when they’re built, each one will have a slightly different look. This is no different than with a class and its instances.

A class contains three major constituents: instance fields to describe its attributes, constructor method to initialize the fields for specific instances of itself, and methods for its behavior.

For the sake of brevity, I will not elaborate on these three things, but I will give an example of them working in harmony.

Consider the following program:

public class Store {
  String productType;
    public Store(String product) {
    productType = product;
  }
    public void advertise() {
    String message = "Selling " + productType + "!";
    System.out.println(message);
  }
  public void greetCustomer(String customerName) {
    System.out.println("Welcome to the store, " + customerName + "!");
  }
}

In this, we define a class called Store. You can see it has one field called productType, a constructor called Store (constructors always have the same name as the class), and two methods, advertise() and greetCustomer(). When we create an actual version of what this class describes, we can initialize the instance field indirectly by initializing the parameter “product”. This is possible because they are equated to each other. We can also call on the methods in order to advertise our product or greet a customer.

Together, these all allow us to describe in a program a store.

Objects

If a class is a blueprint, then every instance of that class would be the real object that it’s describing. These objects are called objects or instances.

Residing in the main method, instances have the same attributes and behaviors as the classes they are created from—only now the attributes are initialized.

To initialize the attributes (i.e., the instance fields), we call the class’s constructor method. The constructor will usually be declared with parameters passed into it, which are then equated to the instance fields. This is incredibly helpful because when we initialize the parameter, we initialize the instance fields of the object.

In the above code extract, we had a constructor that equated the parameter “product” to the instance field “productType.” If we were to create an instance of this class called lemonadeStand, we could initialize productType indirectly by initializing the parameter.

Store lemonadeStand = new Store("Lemonade");

We have just passed the argument “Lemonade” into the place of the parameter, thus initializing it to this value. If we were to print out productType of lemonadeStand, we would see “Lemonade”, but without the quotes.

These simple ideas allow us programmers to do a wealth of things.