1

349 views

Published on

Hariprasanna V (9843824677)

1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
349
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
12
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

1

  1. 1. Day 4 <ul><li>Introduction to: </li></ul><ul><li>Classes & Objects </li></ul><ul><li>Methods </li></ul><ul><li>Constructors </li></ul><ul><li>Garbage Collections </li></ul>
  2. 2. 2 Classes & Objects <ul><li>Classes are collecton of objects </li></ul><ul><li>Thus, a class is template for an object, and an object is an instance of a class. </li></ul><ul><li>A class is declared by use of the class keyword. The general form of a class is : </li></ul>
  3. 3. <ul><li>class classname { </li></ul><ul><li>type instance-variable1; </li></ul><ul><li>type instance-variable2; </li></ul><ul><li>//…. </li></ul><ul><li>type methodname1(parameter-list) { </li></ul><ul><li>//body of method </li></ul><ul><li>} </li></ul><ul><li>//… </li></ul><ul><li>type methodnameN(parametere-list) { </li></ul><ul><li>//body of method}} </li></ul>
  4. 4. . <ul><li>Declaring Objects : Obtaining objects of a class is a two-step process. First u must declare a variable of the class type. This variable does not define an object. Second, you must acquire an actual, physical copy of the </li></ul><ul><li>object ans assign to that variable. You can do this by using the new operator. </li></ul><ul><li>Box mybox = new Box(); </li></ul><ul><li>The new operator dynamically allocates memory for an object. It has the general form: </li></ul><ul><li>Class-var = new classname(); </li></ul><ul><li>Object Reference Variables. </li></ul><ul><li>Box b1 = new Box(); </li></ul><ul><li>b2 = b1; </li></ul>
  5. 5. <ul><li>Introducing Methods : The general form the method is: </li></ul><ul><li>type name(parameter-list){ </li></ul><ul><li>// body of the method </li></ul><ul><li> } </li></ul><ul><li>Methods that have a return type other than void return a value to the calling routine using the following form of the return statement: </li></ul><ul><li>return value; </li></ul><ul><li>here, the value is the value returned. </li></ul><ul><li>Adding a method to the Box class (e.g. refer codes) </li></ul><ul><li>Returning a Value (e.g. refer codes) </li></ul><ul><li>Adding a Method that takes Parameters (e.g. refer codes ) </li></ul>
  6. 6. <ul><li>Constructors : Java allows object to initialize themselves when they are created. This automatic initialization is performed through the use of the constructors. </li></ul><ul><li>A constructors initialization an objects immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to the method. Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes.( e.g. refer codes) </li></ul><ul><li>Parameterized Constructors </li></ul><ul><li>This keyword : this can be used in any method to refer to the current object. This is always a reference to an object on which the method is invoked. </li></ul><ul><li>Garbage Collection </li></ul>
  7. 7. <ul><li>Overloading Methods </li></ul><ul><li>Overloading Constructors </li></ul><ul><li>Using Objects as Parameters. </li></ul><ul><li>Argument Passing : call-by-value & call-by-Reference. </li></ul><ul><li>Returning Objects. </li></ul><ul><li>Recursion. </li></ul><ul><li>static. </li></ul><ul><li>final. </li></ul><ul><li>Arrays Revisited (method to find the length of array) </li></ul><ul><li>Nested classes & Inner Classes. </li></ul><ul><li>Command-Line-Arguments. </li></ul>
  8. 8. Sample Code class BoxDemo { public static void main(String args[]) { Box mybox = new Box(); double vol; // assign values to mybox's instance variables mybox.width = 10; mybox.height = 20; mybox.depth = 15; // compute volume of box vol = mybox.width * mybox.height * mybox.depth; System.out.println(&quot;Volume is &quot; + vol); } }

×