Module 3: Classes and Objects Programming in Java (OE)
Module 3
Introducing Classes
The class is at the core of Java.
It is the logical construct upon which the entire Java language is built because it
defines the shape and nature of an object.
Class Fundamentals
Class defines a new data type. Once defined, this new type can be used to create
objects of that type.
Thus, a class is a template for an object, and an object is an instance of a class.
Because an object is an instance of a class, you will often see the two words object
and instance used interchangeably.
The General Form of a Class
When you define a class, you declare its exact form and nature. You do this by
specifying the data that it contains and the code that operates on that data.
A class is declared by use of the class keyword
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodnameN(parameter-list) {
// body of method
}
}
Mrs Mamatha A, Asst. Prof, Dept., of CSE, SVIT 1
,Module 3: Classes and Objects Programming in Java (OE)
The data, or variables, defined within a class are called instance variables.
The code is contained within methods.
Collectively, the methods and variables defined within a class are called members of
the class.
Variables defined within a class are called instance variables because each instance of
the class (that is, each object of the class) contains its own copy of these variables.
Thus, the data for one object is separate and unique from the data for another.
A Simple Class
Here is a class called Box that defines three instance variables: width, height, and
depth.
class Box {
double width;
double height;
double depth;
}
As stated, a class defines a new type of data.
In this case, the new data type is called Box.
You will use this name to declare objects of type Box.
It is important to remember that a class declaration only creates a template; it does not
create an actual object
Box mybox = new Box(); // create a Box object called mybox
After this statement executes, mybox will be an instance of Box.
Thus, it will have “physical” reality.
Thus, every Box object will contain its own copies of the instance variables width,
height, and depth.
To access these variables, you will use the dot (.) operator.
The dot operator links the name of the object with the name of an instance variable.
For example, to assign the width variable of mybox the value 100, you would use the
following statement:
mybox.width = 100;
Mrs Mamatha A, Asst. Prof, Dept., of CSE, SVIT 2
,Module 3: Classes and Objects Programming in Java (OE)
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);
}
}
Declaring Objects
When you create a class, you are creating a new data type.
However, obtaining objects of a class is a two-step process.
First, you must declare a variable of the class type. This variable does not define an
object. Instead, it is simply a variable that can refer to an object.
Second, you must acquire an actual, physical copy of the object and assign it to that
variable. You 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, more or less, the address in memory of the object allocated by new.
This reference is then stored in the variable.
Thus, in Java, all class objects must be dynamically allocated.
Mrs Mamatha A, Asst. Prof, Dept., of CSE, SVIT 3
, Module 3: Classes and Objects Programming in Java (OE)
Box mybox = new Box();
This statement combines the two steps just described. It can be rewritten like this to
show each step more clearly:
Box mybox; // declare reference to object
mybox = new Box(); // allocate a Box object
The first line declares mybox as a reference to an object of type Box.
After this line executes, mybox contains the value null, which indicates that it does
not yet point to an actual object.
Any attempt to use mybox at this point will result in a compile-time error. The next
line allocates an actual object and assigns a reference to it to mybox.
After the second line executes, you can use mybox as if it were a Box object. But in
reality, mybox simply holds the memory address of the actual Box object.
Here, class-var is a variable of the class type being created. The classname is the
name of the class that is being instantiated.
The class name followed by parentheses specifies the constructor for the class.
A constructor defines what occurs when an object of a class is created.
Constructors are an important part of all classes and have many significant attributes.
It is important to understand that new allocates memory for an object during run time.
The advantage of this approach is that your program can create as many or as few
objects as it needs during the execution of your program.
However, since memory is finite, it is possible that new will not be able to allocate
memory for an object because insufficient memory exists.
If this happens, a run-time exception will occur.
A class creates a new data type that can be used to create objects.
Mrs Mamatha A, Asst. Prof, Dept., of CSE, SVIT 4
Module 3
Introducing Classes
The class is at the core of Java.
It is the logical construct upon which the entire Java language is built because it
defines the shape and nature of an object.
Class Fundamentals
Class defines a new data type. Once defined, this new type can be used to create
objects of that type.
Thus, a class is a template for an object, and an object is an instance of a class.
Because an object is an instance of a class, you will often see the two words object
and instance used interchangeably.
The General Form of a Class
When you define a class, you declare its exact form and nature. You do this by
specifying the data that it contains and the code that operates on that data.
A class is declared by use of the class keyword
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodnameN(parameter-list) {
// body of method
}
}
Mrs Mamatha A, Asst. Prof, Dept., of CSE, SVIT 1
,Module 3: Classes and Objects Programming in Java (OE)
The data, or variables, defined within a class are called instance variables.
The code is contained within methods.
Collectively, the methods and variables defined within a class are called members of
the class.
Variables defined within a class are called instance variables because each instance of
the class (that is, each object of the class) contains its own copy of these variables.
Thus, the data for one object is separate and unique from the data for another.
A Simple Class
Here is a class called Box that defines three instance variables: width, height, and
depth.
class Box {
double width;
double height;
double depth;
}
As stated, a class defines a new type of data.
In this case, the new data type is called Box.
You will use this name to declare objects of type Box.
It is important to remember that a class declaration only creates a template; it does not
create an actual object
Box mybox = new Box(); // create a Box object called mybox
After this statement executes, mybox will be an instance of Box.
Thus, it will have “physical” reality.
Thus, every Box object will contain its own copies of the instance variables width,
height, and depth.
To access these variables, you will use the dot (.) operator.
The dot operator links the name of the object with the name of an instance variable.
For example, to assign the width variable of mybox the value 100, you would use the
following statement:
mybox.width = 100;
Mrs Mamatha A, Asst. Prof, Dept., of CSE, SVIT 2
,Module 3: Classes and Objects Programming in Java (OE)
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);
}
}
Declaring Objects
When you create a class, you are creating a new data type.
However, obtaining objects of a class is a two-step process.
First, you must declare a variable of the class type. This variable does not define an
object. Instead, it is simply a variable that can refer to an object.
Second, you must acquire an actual, physical copy of the object and assign it to that
variable. You 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, more or less, the address in memory of the object allocated by new.
This reference is then stored in the variable.
Thus, in Java, all class objects must be dynamically allocated.
Mrs Mamatha A, Asst. Prof, Dept., of CSE, SVIT 3
, Module 3: Classes and Objects Programming in Java (OE)
Box mybox = new Box();
This statement combines the two steps just described. It can be rewritten like this to
show each step more clearly:
Box mybox; // declare reference to object
mybox = new Box(); // allocate a Box object
The first line declares mybox as a reference to an object of type Box.
After this line executes, mybox contains the value null, which indicates that it does
not yet point to an actual object.
Any attempt to use mybox at this point will result in a compile-time error. The next
line allocates an actual object and assigns a reference to it to mybox.
After the second line executes, you can use mybox as if it were a Box object. But in
reality, mybox simply holds the memory address of the actual Box object.
Here, class-var is a variable of the class type being created. The classname is the
name of the class that is being instantiated.
The class name followed by parentheses specifies the constructor for the class.
A constructor defines what occurs when an object of a class is created.
Constructors are an important part of all classes and have many significant attributes.
It is important to understand that new allocates memory for an object during run time.
The advantage of this approach is that your program can create as many or as few
objects as it needs during the execution of your program.
However, since memory is finite, it is possible that new will not be able to allocate
memory for an object because insufficient memory exists.
If this happens, a run-time exception will occur.
A class creates a new data type that can be used to create objects.
Mrs Mamatha A, Asst. Prof, Dept., of CSE, SVIT 4