Classes And Objects:
Class is a template for an object, and an object is an instance of a class.
Syntax:
class className
{
type instance-variable1;
type instance-variable2;
type instance-variableN;
type methodname1(parameter-list)
{ // body of method }
type methodname2(parameter-list)
{ // body of method }
type methodnameN(parameter-list)
{ // body of method }
}
Example
class Rectangle
{
int height;
int width;
int Area()
{
return height*width;
}
}
Declaring Objects of Class:
Syntax:
className objName = new classNameOrConstructor();
Example:
Rectangle myRec = new Rectangle();
e.g. class Rectangle {
int height, width;
public static void main(String args[])
{ Rectangle myRec = new Rectangle();
myRec.height = 3;
myRec.width = 2;
System.out.println("Area of Rectangle :" + myRec.height*myRec.width); } }
Assigning Object Reference Variables:
When you assign one object reference variable to another object reference variable, you are not creating a copy of the object; you are only making a copy of the reference.
e.g. Rectangle myRec1 = new Rectangle();
Rectangle myRec2 = myRec1;
In above example both myRec1 and myRec2 are referring to a single object. myRec1 is not copied to myRec2 only its address.
1 Comments
Very well
ReplyDeletePost a Comment
Please do not enter any spam link in the comment box