Classes

1. Class

A class is a blueprint for an object. A class is at the very core of the JAVA technology.

Let us take an Example of a bottle manufacturing plant. See Below

Bottle Manufacturing

Here, the bottle manufacturing machine is like the Class and the individual bottles that you see( the nice green ones..) are the actual objects or instances developed from the Machine. Obviously, the production manager must have configured the settings of the Machine/inputs-raw material to generate these kind of bottle. If he needed blue colored bottle instead, he might have used a different material.

As you can see, all the bottles have properties like color, size, shape, volume. These properties are like attributes that all the bottles have.

Also, they can be filled, emptied, thrown, crushed(difficult if it is made of glass). These are the  operations that can be done on them or associated with all the objects.

Linking it to Java World

Bottle Class

The syntax goes like this

accessModifier class <classname> { }

Example:

“public class Bottle{

}”

public is optional.

Everthing inside the 2 curly braces are the attributes and behaviour/methods of the class.

Your Bottle class now have 4 attributes bottleId, color, volume and size. The attributes holds the detail of the state of the object.

Also, the two operations that can be done on a bottle object are fill and empty. These operations are also called as methods and define the behaviour of the object.

We will talk about constructors, package and other things in other posts.