Tuesday, August 10, 2010

Object Oriented Programming (OOP) Aug 10th

print and println:

print------ cursor goes to the next line(similar to \n)
println---- Cursor remains in the same line

Class- In OOP a class is a template defination of methods and variables in a particularkind of objects.

Hence, we can say that class defines the structures and behaviors(data and code) that will be shared by a set of objects.

Objects- Object is the instance of class

A program from schieldt


/* A program that uses the Box class.
Call this file BoxDemo.java
*/
class Box {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
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("Volume is " + vol);
}
}

So, to create an object

Box mybox=new Box();
where Box() is the constructor or class name as both should be same.

statement and funtion:


Box mybox2=mybox; does not create an object rather a reference is created.

If we change the value of any parameters like width,height or depth by mybox2, then the change will also stay if we call by mybox.

No comments:

Post a Comment

Featured Post 1

notice

Featured Post 2

IETE

Featured Post 6

results

Featured Post 7

WBUT

Featured Post 3

Featured Post 4

Featured Post 5

Featured Post 8