Chapter6 Classes
Chapter6 Classes
What is Class?
Class is core-basic construction upon which
entire Java language is built.
The class is an template for an object.and an object is an instance of an class.
When you define class you declare its exact form and nature because you
define data members and member functions of that class
General form :
class classname
{
Type instance – variable 1;
Type instance – variable 2;
//
Type instance variableN;
Type methodnameN(paramer-list){
Body of method;
}
}
The data or variables defined within the class are called
instance variables.
Because Each instance(each object) of the class contains its own
copy of variables.
So the data for one object is separate from data for another.
Code is contained within methods.Collectively,the methods and
variables defined Within a class are called members of a class
Not all methods like main(),will have public or static.
General form of class does not specify main method.-main method
is not Compulsory For all classes.
Only one of its class – to be starting point of program,should have
main() method.
Further ,applets don’t require main() method at all.
After a long run all declaration and definition – at one place makes it easier
to maintain.
A Simple class
class Box{ ///declares class with 3 instance variables
double width;
double height;
double depth;
}
//Creating object
box mybox = new Box(); // create a box object called mybox
mybox.width = 100;
Create main() method and then you have to call this to initailize the width,height and
depth.
Width
Mybox = new Box(); Height
Depth
mybox
Box object
Object references appear to be similar to pointers .
This suspiction is essentially correct.
An object reference is similar to a memory pointer.
Main difference – is that you cannot manipulate references as you
can actual Pointers.
Thus you cannot cause an object reference to point to an arbitary
Memory location or manipulate it like an integer.
A closer look at new
Class-var = new classname() // dynamially
allocates moemory to object(class variable)
General form:
type name(parameter-list )
return value;
Most of the times you will use methods to access the instance
variables Defined by the class.
Void volume(){
System.out.println(“Volume is :”+width*depth*height);
}
//Call method :
mybox1.volume(); // volume of mybox1-values of width hight
and depth of mybox1
mybox2.volume();//volume of mybox2
Adding a method that takes parameters :
int square ( int i )
{
return i*i ;
}
Example :
Box()
{
width = 10;
height =10;
depth = 10;
}
If you will create two box objects,then they both will have all
three instance variable Values,i.e.width = 10,height =
10,depth=10;
Classname(parameter1,parameter2,…)
{
var1 = parameter1;
var2 = parameter2;
//…so on
}
The this keyword
Sometimes a method will need to refer to
the object that invoked it.
To allow this Java defines the this keyword
this can be used inside any method to refer to the current object.
That is this always a reference to the object on which the
method was invoked.
You can use this anywhere a reference to an object of the class type
is permitted.
Box(double w, double h,double d)
{
this.width = w;
this.height = h;
this.depth = d;
}
Example
Class Box
{ double width;
double height;
double depth;
}
//Constructor for box
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
Instance variable hiding
You cannot have two local variables with the same name
inside the same or enclosing scopes.
By creating a class , you are creating a new data type that defines
both the nature of the data being manipulated and the routines
used to manipulate it.
The first plate put on the table is the last plate to be used.
But for this implementation you don’t need to worry about the
detailing.