Defining Classes for Objects
Tags: java, programming, learning, beginners
Published: 2024-06-03 16:20:05

Object-oriented programming enables you to develop large-scale software and GUIs effectively. A class defines the properties and behaviors for objects. Object-oriented programming (OOP) involves programming using objects. An _object_ represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique identity, state, and behavior.

- The _state_ of an object (also known as its _properties_ or _attributes_) is represented by _data fields_ with their current values. A circle object, for example, has a data field **radius**, which is the property that characterizes a circle. A rectangle object has the data fields **width** and **height**, which are the properties that characterize a rectangle.
- The _behavior_ of an object (also known as its _actions_) is defined by methods. To invoke a method on an object is to ask the object to perform an action. For example, you may define methods named **getArea()** and **getPerimeter()** for circle objects. A circle object may invoke **getArea()** to return its area and **getPerimeter()** to return its perimeter. You may also define the **setRadius(radius)** method. A circle object can invoke this method to change its radius.

Objects of the same type are defined using a common class. A _class_ is a template, blueprint, or _contract_ that defines what an object’s data fields and methods will be. An object is an instance of a class. You can create many instances of a class. Creating an instance is referred to as _instantiation_. The terms _object_ and _instance_ are often interchangeable. The relationship between classes and objects is analogous to that between an apple-pie recipe and apple pies: You can make as many apple pies as you want from a single recipe. Figure below (a) shows a class named **Circle** and its three objects.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lykovayho4ex5w4wpk9.png)

A Java class uses variables to define data fields and methods to define actions. Additionally, a class provides methods of a special type, known as _constructors_, which are invoked to create a new object. A constructor can perform any action, but constructors are designed to perform initializing actions, such as initializing the data fields of objects. Figure below (b) shows an example of defining the class for circle objects.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aohdhze6vbeq62l952g9.png)

The **Circle** class is different from all of the other classes you have seen thus far. It does not have a **main** method and therefore cannot be run; it is merely a definition for circle objects. The class that contains the **main** method will be referred to in this book, for convenience, as the _main class_.

The illustration of class templates and objects in Figure above (a) can be standardized using _Unified Modeling Language (UML)_ notation. This notation, as shown in Figure below, is called a _UML class diagram_, or simply a _class diagram_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ykn2d2jwgfwvnq7iuigr.png)

In the class diagram, the data field is denoted as

`dataFieldName: dataFieldType`

The constructor is denoted as

`ClassName(parameterName: parameterType)`

The method is denoted as

`methodName(parameterName: parameterType): returnType`