Objects and Classes in Java

Facebooktwitterredditpinterestlinkedinmailby feather

Objects and Classes in Java

Classes and objects are the basic concepts of object-oriented programming. It revolves around the real world entity. In Java, the object is a physical and logical entity, whereas classes are logical entity only.

Objects in Java-

Objects are real-world entities that have state and behavior. Objects can be physical and logical. It is the instance of the class. Some example of objects are-

We can say objects are-

  • Real-world entities
  • Instances of the classes
  • Tangible or intangible
  • Entities which have state and behavior

Objects can be tangible or intangible both. The banking system is the example of the intangible object. Object corresponds to things found in the real world.

Every object has some characteristics like its name, method, and other information about it. Characteristics of an object are-

  • State
  • Behavior
  • identity

State- it contains the attribute and value of an object. It is the reflection of object properties.

Behavior- it contains methods of an object. It is the response reflection of an object to another object. It represents the functionality of an object like deposit, etc.

Identity- identity is a unique id. It is used internally by the JVM to identify the uniqueness of the object.

Example of an object

Object Car

Identity- 

Object name- car

Attributes-

Brand

Model

Color

speed

Behavior-

Run

Speed

Object declaration-

As an object is called an instance of the class so, when an object is created in a class, class is said to be instantiated. All the instances share the attributes and properties of the class, but the values of these attributes (state) are unique. A single class can have any number of instances.

Example-

Output-

Ways to create an object of a class-

  • Using a new keyword
  • Using class.forName(String  className) method
  • Using clone() method
  • Deserialization

Using new keyword-

It is most common and used way to create object in java.

Using Class.forName(String className) method-

In JAVA, there is a predefined class Class in java.lang package. The forName() method is a Java Class method. This method returns the class object associated with the class or interfaces with the given name in the parameter as String.

Consider class Demo present in pack1 package.

Using clone() method-

clone() method is an Object class method. This method creates and returns a copy of the object class.

Using Deserialization-

Deserialization is a process of reading an object from a saved file.

Classes in java-

Classes are the user-defined blueprint of objects. It contains a set of properties and method that are common to all objects of one type.

We use the keyword class for creating a class.

Syntax-

A Java class contains-

  • Methods
  • Fields
  • Constructors
  • Blocks
  • Nested classes and interfaces

Methods in Java-

In Java, a method is a block of code meant for any certain operation. It runs when it calls. Methods can be called anywhere in the program. Methods are like functions.

Advantages-

Code re-usability is the main advantage of methods we can use the code many times by defining only once.

Code optimization is also an advantage of methods. We can reduce the line of code of a program by defining methods.

To create a method-

Methods should be defined within the class. It should be determined by method name followed by parenthesis (). There are some predefined methods in java, like System.out.println(). An example of creating method is below.

In the given example we have defined method ‘method1’ within the demo class.

To call a method-

To call a method write method name followed by parenthesis () and thereafter semicolon ‘; ‘.  Like in the below program, I defined method1 for printing “in method 1”. When I will call the method1() it will give the output as “in method 1”.

Output-

Fields-

A class field may contain many things like variables constant and other data types.

A class contains any of the following variables-

  • Local variables
  • Class variable
  • Instance variable

Local variable-

Local variables are defined within the methods, constructor, or blocks. These variables are initialized within the block and destroyed when the method is completed. 

Class variable-

Class variables are defined within the class outside any methods with the static keyword.

Instance variable-

An instance variable is defined within the class outside any methods. These variables are initialized when the class is initialized. Instance variable can be accessed anywhere in the program.

Constructors-

One of the most important subtopics of the class is a constructor. Every class contains a minimum of one constructor. If we do not define it manually, then Java compiler will create a constructor by default.

When a new object is initialized, a constructor is invoked. It initializes the objects. Its name is as class name. More than one constructor can be created in a single class.

Let’s have a look at the below example-

Block in Java-

A block contains one or more statements enclosed in braces. Generally, a block is used to group several statements so that they can be used on the requirement.

Below is an example of a simple block.

Java example for objects and classes-

Example1-

This is a simple example for how to define class and fields.

Output-

Example2-

For initialization through reference variable

Initialization means storing the data into a variable. In this example, we will store the value into an object through the reference variable.

Output-

Example3-

Initialization through method-

In this example, we will create two objects of the Shop class and initializing the value of these objects through methods.

Output-

Facebooktwitterredditpinterestlinkedinmailby feather