Classes and objects are one of the most important areas to keep track of in programming. Classes and objects are the building blocks of larger programs that allow you to program good models of reality.
Classes and objects form the basis for larger programs. In the next chapter we will discuss the difference between classes and objects and why it is important to keep the concepts apart. But first, we take an introduction to why we need to master programming with classes and objects.
Programming is about being able to create a model of reality. In fact, a program is several small building blocks (objects) that together build large programs. This is what is called object-oriented programming, in other words, a program consists of several objects that work together to solve a task.
You say you want a certain kind of so called abstraction in programming. In other words, you don’t want to code something that does the exact same operation somewhere else. That’s why you create classes and objects. The idea is that each class should manage their part of the program and be completely independent of each other.
At first it could be quite difficult to understand the difference between a class and an object. Therefore, in the following chapter we will go into details about what classes and objects are and how to create them. But broadly speaking, the difference is:
Classes: determine the features (methods) and attributes (characteristics) an object should have.
Object: consists of the information that describes the properties and attributes specified in the class.
Imagine that we wanna program a cat – a class called cat. A cat has certain characteristics, such as being able to scratch or run. A cat also has certain attributes, such as a name and a color. In this example, the cat cat has two methods: scratch and run. It also has two attributes (states): name and color.
We now have a cat template. From this template we can create several cats, that is, objects of the cat class. It’s called that you create an instance of the cat class. One cat may be called Lucy and has a brown fur, while the other cat may be called Tom and has a white colored fur. So we have two items of the cat class. One object is called Lucy and the other is Tom, but both objects have the ability to scratch and run. Both are cats!
Click on picture to get a larger image
Now we have looked at an introduction to classes and objects in Java, and why these are important.
On the coming pages we will go into detail how to create classes and objects and what to consider. Furthermore, for more technical and specific information about Classes and Objects in java we recommend the Oracle website