** Introducing classes**
3/5/17
- Class is the core of java.A class forms the basis for the object oriented in programming in Java.
- Here we learn about methods,constructors and the this keyword.
Class fundamentals:
- Class defines new datatype.Class is template of an object and an object is an instance of class.
- A class is declared by class keyword.A class code contain interfaces(inherit abstract class) to its data.
- The syntax:
class classname{
type instance-variable1;
//....
type instance-variable2;
type methodname1(parameter-list){
//....
body of the method
type methodname2(parameter-list){
//....
body of the method
}
}
- The java class does not specify main() it specify the class with the starting program.Some application such as applet not need main() method.
Simple class:
- In these class defines the instance variable as height,width & depth.
To create box object
box mybox = new box();
The dot operator combines the name of the instance object and the name of the instance variable.
mybox.width=100
Due to the main() is obtained it is called Classdemo.java
Example:
class box{
double width;
double height;
double depth;
}
public class HelloWorld{
public static void main(String []args){
box mybox=new box();
double vol;
mybox.width=10;
mybox.height=20;
mybox.depth=15;
vol=mybox.depth*mybox.height*mybox.depth;
System.out.println\("Volume is"+vol);
}
}
Declaring objects:
You create a new class
The new operator dynamically allocates the memory of an object and returns a reference to it.
A variable of class type and refer to the object.
This reference is more or less the address in the memory of the object allocated new
box mybox Declare a reference to the object.
mybox=new box allocate a box object.
mybox is the reference of the box type.And it is not referred to actual type.
Second line allocates the box type and assign referred to object type.
Closer Look New:
A class create a new data type that can be used to create objects.
New operator is used to allocate the memory of an object.
General form is,
class-var=new classname();
class var is a variable of class type.A classname is reference or instance of class.
A constructor define what occur when object of class is created.
It simply explicit the own constructor or else default constructor is applied.
The program create the possible objects needed to run the program.
Assigning object reference:
Box b2=b1;
b1 and b2 will be same object.
b1 and b2 did not allocate any memory or any part of original object.
Since b1 & b2 are same object changes in b2 will make changes in b1.
There is no link between b1 and b2 other than same objects.
b1=null;
b1 sets to null and b2 still points to original object.
Introducing Methods:
General form of method:
typename (parameterlist){
//body of method
}
type specify data returned by method.
name of method specified by name.
Parameter is a list of sequence of type and identified pairs seperated by commas.
Adding a method to box class:
4/5/17
Methods define interface of class.
Examples:
class box
{
double width;
double height;
double depth;
void volume()
{
System.out.print("Volume is");
System.out.println(width*height*depth);
}
}
public class HelloWorld{
public static void main(String []args){
box mybox1=new box();
box mybox2=new box();
mybox1.width=10;
mybox1.height=20;
mybox1.depth=15;
mybox2.width=3;
mybox2.height=6;
mybox2.depth=9;
mybox1.volume();
mybox2.volume();
}
}
mybox1.volume displays the volume of the mybox1.(same as mybox2)
- Each time when volume is invoked it display the specified volume.
- When mybox1.volume is executed it pass the control to the code inside the volume() and
- After volume() is executed resumed from calling code
- Method not referred on instance variable as height,width,depth.It referred by its class.without use of dot operator.
- When instance variable is accessed by code by use dot operator but not defined by the instance variable we defined the class.
- In that case instance variable accessed due to class we defined.