Java Classes and Objects.

Java is an Object-Oriented Programming Language and writing a program in Java is totally different from any other Object-Oriented Programming Language like C++ or Python. In Java, it is necessary to create a class before writing any solution to any programming question. 

Why it is important to create a Class in Java?

The property of the class is to combine all the properties and behaviors of an object in a program into a single unit.  A class is a template or blueprint that describes the behavior/states that an object of its type supports. 

What is an Object?

Any real-world entity with well-defined properties is called an Object. An Object will have a state, behaviors, and unique identity. An object can be tangible (physical entity) or intangible (cannot be touch or feel). 

There are three main characteristics of Objects, 

  • State: Represents the data of an object. 
  • Behavior: Represents the behavior of an object such as deposit, withdrawal, etc. 
  • Identity: It is used internally by the JVM to identify each object uniquely. 

What is a Class?

A class defines its object properties and object behavior through methods. A class is a template for a collection of objects that share a common set of attributes and behavior. In the perspective of Object-Oriented programming, a class is a description of the object. It describes the data that each object possesses and it also describes the various behaviors of the object. 


In the above example, you can see a class name Car and with its three different objects Car1, Car2, and Car3 having their own behavior and identity. 
Example of class:
public class Car {
   String carColor;
   String carBrand;
   String fuelType;
}

Example of object:
Car objCar = new Car();

Attribute Declaration. 

Attributes / Fields are used to declare the properties of a class. These attributes are called instance variables. They can be intrinsic types like int, boolean, etc, or user-defined types. 

example: private double salary;

Method Declaration. 

Methods describe the responsibility of the class. These methods are common for all objects. Hence they are known as instance methods. A method always has a name and a return type and it can accept parameters. 

example:
public void calculateArea(int height) {
     int result = height*height;
  System.out.println(result);
}  

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS