Classes and Objects

4
Classes and objects: Class: A class is a user defined data types that provides convenient method for packing together a group of logically related data items and functions that work on them. In Java, the data items are called fields and functions are called as methods. A class is a template that serves to define its properties. Once the class type has been defined, we can create variables of such type known as instances of classes. Defining a class: class ClassName { [Fields declaration] [Method declaration] } In the above the class keyword tells that the following ClassName is an user defined data type. Fields Declaration: Data is encapsulated into the class by placing data fields inside the body of the class definition. These variables are called as Instance Variables because they are created every time an object of class has been instantiated. Example: class Rectangle { int len; int eid;

description

Java

Transcript of Classes and Objects

Page 1: Classes and Objects

Classes and objects:

Class:

A class is a user defined data types that provides convenient method for packing together a group of logically related data items and functions that work on them. In Java, the data items are called fields and functions are called as methods. A class is a template that serves to define its properties. Once the class type has been defined, we can create variables of such type known as instances of classes.

Defining a class:

class ClassName

{

[Fields declaration]

[Method declaration]

}

In the above the class keyword tells that the following ClassName is an user defined data type.

Fields Declaration:

Data is encapsulated into the class by placing data fields inside the body of the class definition. These variables are called as Instance Variables because they are created every time an object of class has been instantiated.

Example:

class Rectangle

{

int len;

int eid;

}

Method Declaration:

A class with only data fields has no life. We must include code to operate on the data contained in the class using methods.

Page 2: Classes and Objects

Method declaration:

ret_type methodname ( parameter-list )

{

----------------------------

-------------------------------

Method-body;

}

Method declaration has four basic parts.

The type of the value that the method returns (ret_type) The name of the method (methodname). A list of parameters (parameter-list), means the number and type of values

that received by the method. The body of the method that contains a set of statements to implement the

task.

Example:

class Rectangle

{

int len;

int wid;

void getData(int x,int y)

{

len = x;

wid = y;

}

void rectArea()

{

int area = len * wid;

System.out.println(“Area of Rect = “ + area);

Page 3: Classes and Objects

Action Statement Result

Declare Rectangle r1; null

Instantiate r1 = new Rectangle();

r1

r1

Rectangle Object

}

}

The above class Rectangle contains two instance variables len and wid, and two methods getData() and rectArea().

Creating Objects:

The class specification only tells the members of a class, no memory is allocated to the class members. To allocate memory for the members of a class, we must create an object to that class.

An object in java is a block of memory that contains space to store all the instance variables. Objects in java are created using the “new” operator. The new operator creates an object and returns reference to the objet.

For example

Rectangle r1; // declares the object

r1 = new Rectangle(); // instantiate the object.

Both statements can be combined as

Rectangle r1 = new Rectangle();

Note: Each object of a class has its own copy of the instance variables. This means that any changes to the variables of one object will not effect on the variables of another.

Accessing Class Members:

Page 4: Classes and Objects

The members of a class cannot be accessed directly. We must use the object of corresponding class with . (member access operator) to access the members of a class.

Syntax:

objectname.member_name;

Ex:

r1.getData(10,20);