Object And Classes In Java
By AmarSivas | | Updated : 2022-05-27 | Viewed : 42 times

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.
Table of Contents:
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.
public class ClassName {
//fields/variables
//methods
}
Simply, we can define a class like this. Please see the below example for the same.
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
Here in our example, the class contains one variable which is String data Typed.
private String vehicleName;
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.
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.
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.
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.
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. |