Chapter6 IntroducingClasses&Methods

26
UNIT- I CHAPTER IN BOOK- 6 CLASSES & METHODS -K. Indhu

description

chapter

Transcript of Chapter6 IntroducingClasses&Methods

UNIT- ICHAPTER IN BOOK- 6CLASSES & METHODS

-K. Indhu

K. INDHU 2

SYLLABUS COVERED HERE• Introducing Classes and Methods in Java

K. INDHU 3

GOALS1. Introduction2. Syntax for Class & Example for Class3. Example for Class with Two Objects4. Declaring Objects Syntax5. Use of new Operator6. Assigning Object Reference Variables7. Methods Syntax & Example8. Constructors Definition & Example9. Three types of Constructors10. Parameterized Constructors11. This Keyword12. Garbage Collection in Java Runtime Environment13. Finalize() method14. Stack Class Example with Output

K. INDHU 4

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 5

SYNTAX FOR CLASS

K. INDHU 6

EXAMPLE FOR CLASS

K. INDHU 7

EXAMPLE FOR CLASS WITH 2 OBJECTS

K. INDHU 8

DECLARING OBJECTS SYNTAX

K. INDHU 9

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 10

ASSIGNING OBJECT REFERENCE VARS.

K. INDHU 11

METHODS SYNTAX

K. INDHU 12

METHODS EXAMPLE

CLASS’S FIELD DECLARATIONS

METHOD RETURNING DOUBLE

METHOD WITHOUTRETURN VALUE

K. INDHU 13

METHODS EXAMPLE CONTINUED…

CREATE 2 OBJECTS

CALL FUNCTIONWITHOUTRETURN VALUE

CALL FUNCTIONWITH RETURNVALUE DOUBLE

K. INDHU 14

CONSTRUCTORS• DEFINITION:-• Constructors is a SPECIAL FUNCTION->• -> which has same name as that of it’s 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 15

CONSTRUCTORS EXAMPLE

CONSTRUCTOR NAMED BOX IS WRITTEN

new ClassName() automatically invokesConstructor with no parameters

K. INDHU 16

THREE TYPES OF CONSTRUCTORS• (1) DEFAULT 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 17

PARAMETRIZED CONSTRUCTORS

CONSTRUCTOR WITH THREE PARAMETER

K. INDHU 18

“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 19

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 20

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 21

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 22

STACK CLASS EXAMPLE

K. INDHU 23

STACK CLASS EXAMPLE CONTINUED…

K. INDHU 24

STACK CLASS OUTPUT

K. INDHU 25

SO FAR WE STUDIED…• Introducing Classes and Methods in Java

HAPPY LEARNING!!!