You are on page 1of 26

UNIT- I

CHAPTER IN BOOK- 6
CLASSES & METHODS
-K. Indhu

SYLLABUS COVERED HERE


Introducing Classes and Methods in
Java

K. INDHU

GOALS
1. Introduction
2. Syntax for Class & Example for Class
3. Example for Class with Two Objects
4. Declaring Objects Syntax
5. Use of new Operator
6. Assigning Object Reference Variables
7. Methods Syntax & Example
8. Constructors Definition & Example
9. Three types of Constructors
10.Parameterized Constructors
11.This Keyword
12.Garbage Collection in Java Runtime Environment
13.Finalize() method
14.Stack Class Example with Output
K. INDHU

INTRODUCTION

The class is at the core of Java.

It is the logical construct upon which the entire Java


language is built because it defines the shape and
nature (or template) of an object.

Any concept we wish to implement in a Java program


must be encapsulated within a class.

DEFINITION:1. A class is a template for an object.


2. In other words, a Class is a group of objects belonging
to similar kind.
K. INDHU

SYNTAX FOR CLASS

K. INDHU

EXAMPLE FOR CLASS

K. INDHU

EXAMPLE FOR CLASS WITH 2


OBJECTS

K. INDHU

DECLARING OBJECTS SYNTAX

K. INDHU

USE OF NEW OPERATOR

The "new" operator dynamically allocates memory for an


object.

It has this general form:class-variable = new classname( );

Here, class-variable is a variable of the class type being


created.
The classname is the name of the class that is being
instantiated.
The class name followed by parentheses specifies the
constructor for the class.

A constructor initializes class members, when an object of a


class is created.

However, if no explicit constructor is specified, then Java will


automatically supply a default constructor.
K. INDHU

ASSIGNING OBJECT
REFERENCE VARS.

K. INDHU

10

METHODS SYNTAX

K. INDHU

11

METHODS EXAMPLE

CLASSS FIELD DECLARATIO

METHOD RETURNING DOU

METHOD WITHOUT
RETURN VALUE
K. INDHU

12

METHODS EXAMPLE
CONTINUED

CREATE 2 OBJECTS
CALL FUNCTION
WITHOUT
RETURN VALUE
CALL FUNCTION
WITH RETURN
VALUE DOUBLE

K. INDHU

13

CONSTRUCTORS

DEFINITION:Constructors is a SPECIAL FUNCTION->


-> which has same name as that of its class
name and,
-> that do not have return type.
PURPOSE OF CONSTRUCTOR:It can be tedious to initialize all of the
variables in a class each time an instance is
created.
A constructor initializes an object immediately
upon creation.
K. INDHU

14

CONSTRUCTORS EXAMPLE

CONSTRUCTOR NAMED BOX IS WRITTE

new ClassName() automatically invokes


Constructor with no parameters

K. INDHU

15

THREE TYPES OF
(1) DEFAULTCONSTRUCTORS
CONSTRUCTOR->

CONSTRUCTOR WITH NO PARAMETERS

(2) PARAMETERIZED CONSTRUCTOR->

CONSTRUCTOR WITH ONE OR MORE PARAMETERS


HERE, VALUES PASSED THRU PARAMETERS ARE
ASSIGNED TO THE MEMBERS OF THE CLASS.
IF PARAMETER NAME IS EQUAL TO CLASS MEMBER
NAME, THEN USE THIS POINTER TO ACCESS MEMBER
OF CLASS.

(3) COPY CONSTRUCTOR->

COPY CONSTRUCTOR IS NOT THERE IN JAVA LIKE IN C+


+.
BUT, USING PARAMETERIZED CONSTRUCTOR, OBJECT
CAN BE PASSED AS PARAMETER TO JAVA METHOD.
IN THIS WAY ONE OBJECT CAN BE COPIED TO ANOTHER
OBJECT.
K. INDHU

16

PARAMETRIZED
CONSTRUCTORS

CONSTRUCTOR WITH THREE PARAM

K. INDHU

17

THIS KEYWORD

That is, This is always a reference to the object on


which the method was invoked.

// A redundant use of this.


Box(double w, double h, double d)
{
this.width = w;
this.height = h;
this.depth = d;
}

The use of this is redundant, but perfectly correct.


Inside Box( ), this will always refer to the CURRENTLY
INVOKING OBJECT.
K. INDHU

18

USES OF THIS KEYWORD

It is used to have SAME NAME to the parameter and


Class field member name.

Like Below->

// Use this to resolve name-space collisions.


Box(double width, double height, double depth)
{
this.width = width;
this.height = height;
this.depth = depth;
}

It is used to hide the Instance Variable Name.


K. INDHU

19

GARBAGE COLLECTION

Java handles de-allocation of un-used memory, for us


automatically.

THIS TECHNIQUE OF DE-ALLOCATING UN-USED


MEMORY AUTOMATICALLY BY JAVA RUN TIME
ENVIRONMENT, IS CALLED GARBAGE
COLLECTION.

Garbage Collection works like this:1. "When no references to an object exist, that object is
assumed to be no longer needed and the memory
occupied by the object can be reclaimed.
2. There is no explicit need to destroy objects as in C++."
K. INDHU

20

FINALIZE() METHOD

By using finalization, you can define specific actions that


will occur when an object is just about to be reclaimed by
the garbage collector.

Inside the finalize( ) method you will specify those actions


that must be performed before an object is destroyed.

The garbage collector runs periodically, checking for


objects that are no longer referenced.

The finalize( ) method has this general form:protected void finalize( )


{
// finalization code here (or) WIND UP ACTIVITIES DONE
HERE
}

K. INDHU

21

STACK CLASS EXAMPLE

K. INDHU

22

STACK CLASS EXAMPLE


CONTINUED

K. INDHU

23

STACK CLASS OUTPUT

K. INDHU

24

SO FAR WE STUDIED
Introducing Classes and Methods in Java

K. INDHU

25

HAPPY
LEARNING!!!

You might also like