0% found this document useful (0 votes)
9 views19 pages

Class and Object

A class serves as a template for creating objects, which are instances of that class. The document outlines the general structure of a class, including access specifiers, instance variables, and methods, as well as the process for declaring and creating objects in Java. It also explains how to access class members and the importance of method return types and parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views19 pages

Class and Object

A class serves as a template for creating objects, which are instances of that class. The document outlines the general structure of a class, including access specifiers, instance variables, and methods, as well as the process for declaring and creating objects in Java. It also explains how to access class members and the importance of method return types and parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Class and object

• a class is a template for an object


• an object is an instance of a class. Because an object is
an instance of a class
The General Form of a Class

A class is declared by use of the class keyword.


A simplified general form of a class definition is shown here:
class classname {
Access type instance-variable1;
Access type instance-variable2;

// ...
type instance-variableN;
Access type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
Access specifier
• Public
• Private
• Protected
• default
• The data, or variables, defined within a class are called
instance variables.
• The code is contained within methods.
• The methods and variables defined within a class are
called members of the class.
• Instance variables are acted upon and accessed by the
methods defined for that class.
• Methods determine how a class’s data can be
used.
• Variables defined within a class are called
instance variables because each instance of the
class contains its own copy of these variables.
• data for one object is separate and unique from
the data for another.
Declaring objects
• when we create a class, a new data type.
• We can use this type to declare objects of that type.

obtaining objects of a class is a two-step process.


• First, we must declare a variable of the class type.
• This variable does not define an object, that can refer to an object.
• Second, acquire an actual, physical copy of the object and assign it to
that variable. We can do this using the new operator.
• The new operator dynamically allocates (that is, allocates at run time)
memory for an object and returns a reference to it.
• This reference is the address in memory of the object
• This reference is then stored in the variable.
• Thus, in Java, all class objects must be dynamically allocated.
Creating object
Box b;
b=new Box();
• Assigning Object Reference Variables
• Box b1 = new Box();
• Box b2 = b1;
Accessing members of class
To access variables of a class(i.e object), use the dot (.) operator.
The dot operator links the name of the object with the name of an
instance variable.
Objname.membername;
mybox.width = 100;
assign the width variable of mybox the value 100
// This class declares an object of
type Box.
class BoxDemo {
double width,height,depth;
public static void main(String args[]) {
Box m = new Box();
double vol; // assign values to mybox's instance variables
m.width = 10;
m.height = 20;
m.depth = 15; // compute volume of box
vol = m.width * m.height * m.depth;
System.out.println("Volume is " + vol);
}}
• The Java compiler automatically puts each class into
its own .class file.
• each object has its own copies of the instance
variables.
• changes to the instance variables of one object have
no effect on the instance variables of another.
This is the general form of a
method:
type name(parameter-list) {
// body of method
}
Here, type specifies the type of data returned by the method.
This can be any valid type, including class types that you create.
If the method does not return a value, its return type must be void.
The name of the method is specified by name.
The parameter-list is a sequence of type and identifier pairs separated
by commas.
Parameters are essentially variables that receive the value of the
arguments passed to the method when it is called.
If the method has no parameters, then the parameter list will be empty.
Methods that have a return type other than void return a value to the
calling routine using the following form of the return statement:
return value;
Here, value is the value returned.
• There are two important things to understand about returning values:
• The type of data returned by a method must be compatible with the
return type specified by the method.
• For example, if the return type of some method is boolean, you could
not return an integer.
• The variable receiving the value returned by a method (such as vol, in
this case) must also be compatible with the return type specified for
the method
Int sum()
{

---
}

byte b=sum();

You might also like