Introduction to Class In Java

The class is a block of code having both the variable and the function. The variable is called an instance variable of the class and the function is called a method of the class. The instance variable is used to store the data. It can be declared in the class but cannot be initialized in the class. Any number and type of instance variable can be declared in the class. The function is called a method of the class. It is used to program processing on to the variable of the class. The method is defined directly in the class. That is prototype cannot be declared of the class. The method can access all the instance variable of the class.

Syntax of Class In Java :

Example:-

Object of the class in java:-

The class is a declared part only to create the member of the class the variable of the class is used. This variable is called an object. The object may be defined as for instance of the class. It may be defined as the variable of the class in which all the member of the class is created physically in continuous order.

Syntax:-

When the object is created all the member are created in continuous order.

                         Object | Instance variable | Method  

However, according to object-oriented technique, the object is logically divided into two part. One part contains instance variable and other part contain method.

                                         Instance variable

                                              Method

                                              Object

 

Accessing the member of the class:-

All the member of the class is created within the object of the class so to access the member of the class the object is required.

Syantax:-

Example:-

CONSTRUCTOR:-

The constructor is the special method of the class that is used to initialize the object of the class. It is used to create the object of the class. It has following property.

a) It is used to create the object of the class.
b) It is used to initialize the instance variable of the class in the object.
c) It is the first member of the class to be called.
d) It is automatically called when the object of the class is created.
e) It must be the public member of the class.
f) It can take 0 or number of arguments.
g) It has no return type that is it does not return any value, not even void or null.
h) It can be overloaded that is multiple constructors can be written in the class.

TYPES OF CONSTRUCTOR:-

There is two basic type of constructor.

  • Default
  • Parameterized

Default constructor: –

Constructor having no argument is called as default constructor. When the programmer does not define any constructor in the class then the compiler automatically provided a default constructor. However, if any version of a constructor is written accept default then the compiler does not provide a default constructor.

Syntax:-

Parameterized constructor:-

Constructor that takes one or more argument is called as a parameterized constructor. The constructor can take any number and type of argument. It can also take the object of the same class as an argument and the constructor is called a copy constructor.

Syntax:-

Example:-

Destructor:-

A destructor is the special method of the class used to destroy the object of the class. Java has no destructor.

Implementation of copy constructor:-

A copy constructor is a form of a constructor that is used to copy the contains of one object into another. It always takes the object of the same class as an argument.

Example:-

Object assignment:-

Object assignment is the process of assigning one object to the another. In Java the operator is defined only for basic type and does not operate on user define type. The assignment operator is the only operator that has been defined for an object and allow to copy the contains of one object into another. The basic difference between assignment operator and copy constructor is copy constructor allows to copy the contains of one object into another during object Creation whereas assignment operator allows copying the contains after the creation of the object.

Example:-

 

Leave a Reply