What is a Class in Java?

Java Programming is an object-oriented programming language. The concepts of class and object in Java are interlinked. We will take an example of constructing a house to understand the concept of class in Java.

Before constructing a house, the architects draw a plan on a chart. After the plan is completed, the actual construction of the house
begins.

However, the plan on the chart is a blueprint, not an object, because it is not occupying space. The constructed house is alone an object as it occupies space.

Likewise, a class in Java is a blueprint for the object. It defines the specific properties of an object.

For example, the animal category has some common properties, such as four legs and one tail. From the categorization, we can understand that these properties are most common in the animal classification. Importantly, though the ‘animal’ classification specifies specific properties, it is not an object as it does not occupy space.

What is a class in Java?

A class is a template, layout, blueprint, or prototype through which we construct an object. It is a user-defined data type, which includes the data members and member functions.

For example, float empBasic; is the data member.

void calculateTotalPay() is the corresponding member function. 

Here, While data member is the state, function is the behavior.

Data Member: Specific properties that are common or consistent throughout their existence.

Behavior: These are specific properties that are not part of throughout the existence of an object but which use these properties to perform various activities.

For Example: Lion has four legs that stay throughout its existence. But jumping is a behavior that is exhibited as it grows up. 

There are three ways to explain the class definition in Java:

Firstly, 

An entity is an object (For example Lion) of a class (from the ‘animal’ classification), which defines the properties in terms of state and behavior.

Secondly, 

The class in Java is similar to the datatype, ‘structure’ in the C programming language.

Class is a user defined datatype.

In addition to data members, the class also includes member functions.

For Example, float empBasic is the data member, and void displayEmployee() is the member function.

Class defines the properties in terms of data members and member functions.

Data members define the state, and member functions define the behavior of an object.

In simple terms, a class can be defined as a user-defined data type that defines the properties in terms of data members and member functions.

By creating a single class, there is the possibility of generating any number of objects. For instance, under the animal class, there are various types of animals, such as tigers, lions, cats, giraffes, and many more. If we take another example, like a house, we can construct any number of houses from one blueprint. However, properties and behaviors defined inside the class will be exhibited by the objects.

The first letter should be in capitals whenever we write the class name. It is a universal standard.

Java Class Example Program

  • In Java, classes instantiate objects. 
  • Here, creating a new object is possible with its set of behaviors and attributes during the instantiation of a class. 
  • An instance is nothing but an object created using the class blueprint. 
  • We use the ‘new’ keyword along with the name of the class to instantiate a class. 

For example, we use the following code to instantiate the employee class.

Employee e = new Employee();

To connect with our experts, click on the link.

Discover more from BerylSoft

Subscribe now to keep reading and get access to the full archive.

Continue reading