Java Class and Objects3 min read

Java is an Object-Oriented Language. And classes and objects are the fundamental components of OOP’s.
Objects are real-life entity and classes are the blueprint for creating an object.

CLASS

A class is a user-defined blueprint or prototype for creating Objects. It is a logical entity. We can say that classes are categories, and objects are items within each o that category.

A class in Java contain:

  • Methods.
  • Fields.
  • Constructors. 
  • Fields.
  • Interfaces.

For the declaration of a class following components are included:

1. Modifiers:
A class can be public or default access.
2. Class name:
A class should have a name with an initial letter Capitalized.
3. SuperClass(if any)/ Interfaces:
If required to include SuperClass then proceed by the keyword ‘extends’. Using the keyword implements, a class can have more than one interface.
4. Body:
A class boundary is within the curly braces'{}’.

Syntax for class:

Syntax example:


OBJECTS:

It can be defined as real-world entities that have state and behavior. An object is a runtime entity. Eg. bike, Lamp, pen, table, etc. An Object is an instance of a class. It is a physical as well as a logical entity.

Objects has three characteristics:

  • State: It is represented by attributes of an object that is the data value.
  • Behaviour: It represents the functionality that is the behaviour of an object. Example withdraw, deposit, etc.
  • Identity: It is a unique ID given to each Object created so that JVM can identify all the objects uniquely.

Example:
Name of an Object: dog
Identity: Name of the dog.
State: age, color, breed, size, etc.
Behaviour: eat, sleep, bark, run, etc.

Syntax of Object:

Following Example shows the creation of an Object:

Output:


Creating Multiple Objects:

Multiple Objects can be created for the same class.

The following example shows that Objects are created for One class, invoking different variables.

Output:


Example of class and objects in Java:

Output:


Constructor.
OOP in java.