Class in Java


Class

Class represents a group of similar objects. The class binds the data, describe the entity and its associates functioning together. So we can say that class is a set of data member or data function. A class is like an object factory. Whenever anew object is required, it is created using the information in the class. Class contains a information of an object’s internal data and used to reserve space and initlize it when an object is brought into existence, a process is called “Instantiation”.

A class is a collection of objects that contains a set of data items that is State and related functions that is behaviors to operate on the given data items.

Class represents a group of similar objects. A class is a way to bind the data describing an entity and its associate’s functions together. The client of a user defined class is the program that instantiates instances and calls methods of the class.

Instance of a class

An object of the class is called instance of the class. In any language, where each object is created from a class, an object is called an instance of the class. Each object the same class would have the same data type defined with the class name.

Instantiating the class

Creating an instance of a class is sometimes referred to as instantiating the class. class contains details of an object’s internal data and when an object is brought into existence ,is called Instantion.

 

Creating object of the class:

An object is creating from the class. The new keyword is used to create the object of a class in Java. New  keyword allocating dynamic memory of an object. There are three steps to create an object from class.

  1. Declaration:  It is uses class as data type along with an object.
  2. Instantiation: The new keyword is used for allocating an object in the memory.
  3. Initialization: It is used for calling a constructor to initialize attributes of the object.

Syntax:

Abc        a1 =       new                   Abc( );

Class     object       new

                                           operator      constructor

 

Class is a user defines data type:

Object oriented programming allows us to model real world objects. User defined classes combine the data and methods as integrated components user may create a data type and declare certain characteristics and behavior within it. This can be done by using a class. This is reason why a class is referred as user defined data type.

User defined classes combine the data and methods that operate on that data.

 

Difference between user defined and Primitive data type.

User defined data type Primitive data type
  1. These data type are created by the user
  1. These data type are built in data types, which are provided by the system.
  1. They are available in accordance to their visibility modes throughout the program.
  1. These data types are available with the system.

 

Advantages of a class:

1. Class is responsible for the validity of the data.

2. Class can be reused.

3. Implementation details can be hidden.

 

Need of a class

In programming language like Java we need to have a programming concept in which data items should remain confined within an area of its application. In this case data items remain hidden to the outside world and allow some use of the essential features This data hiding is an important concept of OOPs which can be implemented by using class.

 

Defining a class

A class may be defined as given below:

Public class<class name>

{

Type instance variable 1

Type instance variable 2

Type instance variable 3

…………………………..

……………………………..

……………………………….

Type method name 1(Parameter list)

{

//body of the method

}

Type method name 2()

{

//body of the method

}

While defined a class following items are essentially required

1. Public           : Public is access sepecifier which indicates the extent of usage of class    members.

2. Class            : Class is a keyword applied to declare a class.

3. Class name  :   This is referred as a tag which signifies user defined type. Any variable declared of this type is the object .It is         also known as Instance of the class.

4. Instant Variables : The variables which are declared within the a class, are called instant variables. The instant variables are    applied in the member functions.

5. Member Methods:  The methods which are defined within a class are termed as member methods. These methods basically deal with the instant variables of the objects.

 

Content of a class

There are two types of contents.

  1. External Wrapper
  2. Internal  Contents

 

External Wrapper

It is the class declaration enclosing inner part within a pair of curly brackets.

Class abc

{

///inner parts of a class

}

 

Internal contents of a class

Inner part of the class contains the following:

  1. Instant Variables
  2. Constructor
  3. Member methods

 

Access Specifiers:

These are also known as visibility modes. In java these are

  1. Public
  2. Protected
  3. Private
    Public:

The class members and methods are specified as Public can be used even outside the visibility of a class.

Syntax: for class members

< Access specifier> <data type><variable>

e.g.     public int a,b,c;

or,

int a,b,c;

Syntax: for class methods

< access specifier> <data type><variable>

e.g.     public putdata ();

or,

void putdata ();

In Java, a class member is declared with no access specifier then it is referred as public by default.So, there is no need to declare the method public.

 

2. Protected:

The protected members are used in the class as private members which can only be applied within the class but can be applied to another class during inheritance.

Syntax: for declaring protected data members

< access specifier> <data type><variable>

e.g.     protected  int a,b,c;

Syntax: for defining a protected  member methods

< Access specifier> <data type><variable>

e.g.     protected  putdata();

 

3. Private:

The class members and methods are specified as Private cannot be used outside the visibility of a class.       

Syntax: for class members

< Access specifier> <data type><variable>

e.g.     private  int a,b,c;

or,

int a,b,c;

      Syntax: for defining a private member method

< Access specifier> <data type><variable>

e.g.     private putdata();

 

 

Referencing member methods:

Member methods can be referenced from the main class through the class objects .e.g.

Class add

{

int a, b,c;

void getdata (int x, int y)

{

A=x;

B=y;

}

void calcu ()

{

c=a+b;

}

void show()

{

System.out.print (“Sum of the numbers=”+c);

}

}

Class xyz

{

Public static void main (String arg [])

{

add a1= new add ();

add a2=new add ();

a1.getdata (2,4);

a1.calcu ();

a1.show ();

a2.getdata (2, 4);

a2.calcu ();

a2.show ();

}

}

In above example the functions getdata (), calcu (), show () are referenced from class objects a1 and a2 through main class abc. These functions are operated one after other by using the instances of class sum a1 and a2.

 

New operator

New operator is used to create a class object or an Array. It allocates memory and returns the reference of memory at run time.

For example

Abc a1= new Abc();

Abc a2= new Abc();

We can use two object of same type.

Comments are closed.