OOP Chapter 2
OOP Chapter 2
1
Objects and Classes
OBJECT VARIABLES
Object: provides a particular basis for computer applications.
2
CONT’D…
You might think that b2 is being assigned a reference to a copy of the
object referred by b1. That is, you might think that b1 and b2 refer to
separate and distinct objects. However, this would be wrong. Instead, after
this fragment executes, b1 and b2 will both refer to the same object. The
assignment of b1 to b2 did not allocate any memory or copy any part of
the original object. It simply makes b2 refer to the same object as does b1.
Thus, any changes made to the object through b2 will affect the object to
which b1 is referring, since they are the same object. This situation is
depicted here:
Although b1 and b2 both refer to the same object, they are not linked in
any other way. For example, a subsequent assignment to b1 will simply
unhook b1 from the original object without affecting the object or affecting
b2. For example:
3
CONT’D…
Box b1 = new Box();
Box b2 = b1;
// ...
b1 = null;
Here, b1 has been set to null, but b2 still points to the original object.
Object variables:
Object variables are declared by stating the class name or data type and
then the variable name same as primitives. Objects are complex variables.
They have an internal state and various behaviors that can either change
the state or simply tell something about the object.
4
CONT’D…
Example:
public void objectVariables() {
Rectangle rect1;
Rectangle rect2;
// 2 Rectangle objects exist??
// more code to follow}
Object variables in Java are actually references to objects, not the objects
themselves. object variables store the memory address of an object of the
proper type not an object of the proper type. contrast this with primitive
variables. rect1 and rect2 are variables that store the memory addresses of
Rectangle objects.
5
DEFINING A CLASS
Class: Describes a set of related objects.
Classes:
6
CONT’D…
Example:
Class animal {
public int noOfLegs;
public String name;
private char gender;
public void showData()
{
display(“Name : “ +name);
display(“Number of Legs :” +noOfLegs);
display(“Gender :” +gender);
}
}
7
MEMBER FUNCTION:
1. It is message to an object.
2. It is usually declared public.
3. Should not have the same name as a data member.
4. Provides access to the data members of a class.
animl.name = “Zebra”;
Calling a member functions:
animl.showData(); 8
INSTANTIATING AND USING
OBJECTS
Instantiating: In Java, this means creation
Creating Objects:
Declaring object variables does not create objects. It is sets aside space to hold
the memory address of an object. The object must be created by using the new
operator and calling a constructor for that object.
Example:
class Rectangle
{
int length;
int width;
Rectangle(int a,int b)
{
length=a;
width=b;
}
int area()
{
14
return(length*width);
CONT’D…
public static void main(String[] args)
{
Rectangle rect=new Rectangle(40,50); // calling constructor
int x=rect.area();
System.out.println("Area is:"+x);
}
}
Output :
Area is : 2000
15
INTRODUCING METHODS
classes usually consist of two things: instance variables and methods. The
topic of methods is a large one because Java gives them so much power
and flexibility.
This is the general form of a method:
type name(parameter-list)
{
// body of method
}
16
CONT’D…
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. This can be any legal identifier other than those already
used by other items within the current scope. 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. In the next few sections, you will see how
to create various types of methods, including those that take parameters
and those that return values.
17
ADDING A METHOD TO THE BOX CLASS
18
EXAMPLE:
This program includes a method inside the box class.
class Box
{
double width;
double height;
double depth;
// display volume of a box
void volume() {
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
19
CONT’D…
Class BoxDemo3 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// display volume of first box
mybox1.volume();
// display volume of second box
mybox2.volume(); 20
CONT’D…
Output :
Volume is 3000.0
Volume is 162.0
The first line here invokes the volume( ) method on mybox1. That is, it
calls volume( ) relative to the mybox1 object, using the object’s name
followed by the dot operator. Thus, the call to mybox1.volume( ) displays
the volume of the box defined by mybox1, and the call to
mybox2.volume( ) displays the volume of the box defined by mybox2.
Each time volume( ) is invoked, it displays the volume for the specified
box.
If you are unfamiliar with the concept of calling a method, the following
discussion will help clear things up. When mybox1.volume( ) is executed,
the Java run-time system transfers control to the code defined inside
volume( ). After the statements inside volume( ) have executed, control
is returned to the calling routine, and execution resumes with the line of
code following the call. In the most general sense, a method is Java’s way
of implementing subroutines.
21
CONT’D…
There is something very important to notice inside the volume( ) method:
the instance variables width, height, and depth are referred to directly,
without preceding them with an object name or the dot operator. When a
method uses an instance variable that is defined by its class, it does so
directly, without explicit reference to an object and without use of the dot
operator. This is easy to understand if you think about it. A method is
always invoked relative to some object of its class. Once this invocation has
occurred, the object is known. Thus, within a method, there is no need to
specify the object a second time. This means that width, height, and
depth inside volume( ) implicitly refer to the copies of those variables
found in the object that invokes volume( ).
Returning a Value:
The volume of a box, implement volume( ) is to have it compute the
volume of the box and return the result to the caller. The following
example, an improved version of the preceding program, does just that:
22
EXAMPLE:
// Now, volume() returns the volume of a box.
class Box
{
double width;
double height;
double depth;
// compute and return volume
double volume() {
return width * height * depth;
}}
23
CONT’D…
class BoxDemo4
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15; 24
CONT’D…
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}}
25
CONT’D…
As you can see, when volume( ) is called, it is put on the right side of an
assignment statement. On the left is a variable, in this case vol, that will receive
the value returned by volume( ). Thus, after
vol = mybox1.volume();
executes, the value of mybox1.volume( ) is 3,000 and this value then is stored
in vol.
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. One more
point: The preceding program can be written a bit more efficiently because there
is actually no need for the vol variable. The call to volume( ) could have been
used in the println( ) statement directly, as shown here:
System.out.println("Volume is " + mybox1.volume());
26
In this case, when println( ) is executed, mybox1.volume( ) will be called
End of CH_2
27