Object And Classes In Java

By | | Updated : 2022-05-27 | Viewed : 42 times

Object And Classes In Java

When we discuss the programming language, we thoroughly hear the Classes and Objects. You can understand how important are these Classes and Objects are in a programming language such as Java. We will explore here this basic concept, i.e., Objects and Classes in Java.

Class In Java

A Class is the blueprint of the object. In simple terms, class is the logical form of the object. You may hear about the blueprint of a building or hotel. This means the blueprint is a prototype of the building or a hotel. So once you see that blueprint, you can understand easily all about the building or hotel, such as design, structure and architecture. In the same way, the class represents the Object in Java. So when you compile and run the class, then objects will be created.

Declare A Class In Java

To declare the Class in Java we need a set of rules/syntaxes to be followed. First of all, we will write a simple class then we will try to get an idea about the syntaxes.

We generally define the class like the below in Java.

ClassName.java
public class ClassName {
	//fields/variables
	//methods
}

Simply, we can define a class like this. Please see the below example for the same.

Vehicle.java
public class Vehicle {
	private String vehicleName;
	public String getVehicleName(){
		return this.vehicleName;
	}
}

Have you noticed here? the public, class, private, string, and return. What are they in Java? How do we use this in the Java language when defining a class.

Components Of Class In Java

Keywords: You noticed these words like public, class, private, string, and return in the class definition. These words are called keywords. These keywords have a separate meanings in a language like Java.

Class: To define the class, the class keyword should be prefixed to the class name. Notice in our example that also we prefixed class keywords to Audi.

Access Modifiers: public/private/protect are keywords and also called Access Modifiers in Java. These keywords can be applicable for classes, methods, and variables.

Class Name: class name can be anything. But there are certain rules which are recommended class definition

Variables: Variables (can also be called fields) will be used for storing different data types. These variables also can be prefixed with Access Modifiers.

Here in our example, the class contains one variable which is String data Typed.

Variables In Java
private String vehicleName;

Methods: We are writing some logic to perform on the data. For this, these methods are defined. When we execute the program these methods will be executed according to our definition of the class. Notice the above example the getVehicleName() is the method. We will get explore in detail the below section about the methods/variables.

Methods In Java
public String getVehicleName(){
	return this.vehicleName;
}

Objects In Java

An instance of the class is called an object such as a book, car, or bike. The object contains the state, behavior, and identity. The object concept is the main pillar of Object-Oriented Programming. The object could be any entity that should have a state and behavior.

Object's Characteristics:

We learned in the above section that an object has three characteristics such as state, behavior, and identity.

State: Object contains data in the form of different types of Fields/Variables. These data fields represent the object's state. These fields can be modified to change the object's state.

Behavior: Object contains different types of methods which are represented the object's behavior. Suppose the consider an Account is an object. Account objects contain methods such as withDraw(), and deposit(). The methods in the object, perform the different types of operations. Here operations are nothing but the Object's Behavior.

Identity: Each object has a unique value to identify the Object. This identity will not be disclosed. JVM will use this identity value in programming the execution.

Instantiating Object In Java:

We discussed what is object in the above section And we will learn here how to instantiate the object for a class. Please notice the below syntaxes.

Instantiating Object In Java
ClassName objectVariable = new ClassName();

Here new is the keyword to create the object. We have other different approaches/ways to instantiate the object. Please see the example below for more understanding.

Object Creation In Java
Vehicel audi = new Vehicle();
Vehicel benz =  new Vehicle();

Class and Object Example In Java

We tried to get an idea of the object and class. We better write one full example to understand better. Please see the below given example. We will improvise the above class.

Class and Object Example In Java
public class Vehicle {
    private String vehicleName;
    public Vehicle(String vehicleName) {
        this.	vehicleName =  vehicleName;
    }
    public String getVehicleName(){
        return this.vehicleName;
    }
    public static void main (String[] args) {
        Vehicle audi = new Vehicle("Audi");
        String vehicleName = audi.getVehicleName();
        System.out.println(" The vehicle name: " + vehicleName ); // output in the console.
    }
}

Difference Between Class and Object

Object Class
The Object is an entity of the class. Class is a prototype/blueprint of the object.
The object can be a physical one. The Class is not a physical one.
The object is the form of the data. So when object creation happen then data will be created in the system as memory. In the creation of the class, the data creation will not happen.
Objects can be created in many ways. The class can be created only one way which is using with class keyword.
Leave A Reply