6_7_Introducing Classes [Autosaved]
6_7_Introducing Classes [Autosaved]
Introducing Classes
Class Fundamentals….
2
• A class is a template that defines the form of an object.
• It specifies both the data and the code that will operate on that data.
• Objects are instances of a class.
• Thus, a class is essentially a set of plans that specify how to build an
object.
• The methods and variables that constitute a class are called members
of the class.
• The data members are also referred to as instance variables.
The General Form of a Class….
• A class is created by using the keyword class. The general form
3
of a
class definition is shown here:
<access modifier> class classname
{
<access modifier> type instance-var1;
<access modifier> type instance-var2;
// ...
<access modifier> type varN;
<access modifier> type method1(parameters)
{
// body of method
}
<access modifier> type method2(parameters)
{
// body of method
}
Access Modifiers 4
This class is called Box, and that defines three instance variables:
width, height, and depth
6
• 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 general form of the dot operator is shown here:
object.member;
• For example, to assign the width variable of mybox the value
100, use the following statement
mybox.width = 100;
/* A program that uses the Box class. Call this file BoxDemo.java */
class Box
8
{
double width;
double height;
double depth;
}
class BoxDemo
{
public static void main(String args[])
{
Box mybox = new Box();//create box object
double vol; Volume is 3000.0
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
// This program declares two Box objects.
class Box 9
{
double width;
double height;
double depth;
}
class BoxDemo2
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
Statement Effect
Box mybox; //declare reference to object null
mybox
This variable does not define an object.
Instead, it is simply a variable that can refer to an
object.
Width
b1 Height
Depth
b2 Box Object
Adding a Method to the Box Class…
13
general form of a method:
type name(parameter-list)
{
// body of method
}
class Box
{
double width;
double height;
double depth;
void volume()
{
System.out.println(" Volume is : ");
System.out.println(width * height * depth);
}
}
class BoxDemo
{ myBox1& 14
public static void main(String args[]) myBox2 are
{ Object for class
Box myBox1 = new Box(); Box
Box myBox2 = new Box();
myBox1.width = 10;
myBox1.height = 20;
myBox1.depth = 15; Displays the volume of the box
defined by myBox1
myBox2.width = 3;
myBox2.height = 6;
myBox2.depth = 9;
Displays the volume
of the box defined by
myBox1.volume();
myBox2
myBox2.volume();
}
}
Returning a value…
15
class Box
{
double width;
double height;
double depth;
double volume()
{
return width * height * depth;
}
}
class BoxDemo
{
public static void main(String args[]) 16
{
Box myBox1 = new Box();
Box myBox2 = new Box();
double vol;
myBox1.width = 10;
myBox1.height = 20;
myBox1.depth = 15;
myBox2.width = 3;
myBox2.height = 6; compute the volume of the box
myBox2.depth = 9; and return the result to the
caller.
vol = myBox1.volume();
System.out.println(“Volume is “ +vol);
// System.out.println(“ Volume is “ + myBox1.volume());
vol = myBox2.volume();
System.out.println(“Volume is “ +vol);
// System.out.println(“ Volume is “ + myBox1.volume());
}
}
17
int x, y ;
x = square(5);
x = square(9);
y = 2;
x = square(y);
19
Constructors
• A constructor initializes an object immediately upon
creation.
•It has the same name as the class in which it
resides and is syntactically similar to a method.
• Once defined, the constructor is automatically called
immediately after the object is created.
• Constructors have no return type, not even void.
class Box
{ 20
double width; Constructor
double height;
double depth;
Box ()
{
System.out.println(" Constructing Box ");
width = 10;
height = 10;
depth = 10;
}
double volume()
{
return width * height * depth;
}
}
class BoxDemo6
{ 21
vol = mybox1.volume( );
System.out.println(" Volume is "+vol);
vol = mybox2.volume( );
System.out.println(" Volume is +vol”); Constructing Box
Constructing Box
} Volume is 1000.0
Volume is 1000.0
}
22
vol = MyBox.volume( );
System.out.println(" Volume is "+vol);
• When this is the case, the methods are said to be overloaded, and
the process is referred to as method overloading.
void trail()
{
System.out.println(" No parameters");
}
void trail(int a, int b)
{
System.out.println(" a and b :"+a+" "+b);
}
void trail( double a)
{
System.out.println(" Inside trail(double) a:"+a);
}
}
class Overload
{ 32
int a, b;
Test(int i, int j)
{
a = i;
b = j;
}
// return true if o is equal to the invoking object
boolean equals(Test o)
{
if(o.a == a && o.b == b)
return true;
else
return false;
}
}
class PassOb 37
{
public static void main(String args[])
{
Test ob1 = new Test(100, 22);
Test ob2 = new Test(100, 22);
Test ob3 = new Test(-1, -1);
System.out.println("ob1 == ob2: " + ob1.equals(ob2));
System.out.println("ob1 == ob3: " + ob1.equals(ob3));
}
}
Argument Passing 38
{
i *= 2;
j /= 2;
}
class CallByValue
{
public static void main(String[] args) {
Test ob = new Test();
int a = 15, b = 20;
System.out.println("a and b before call: " +a + " " + b);
ob.meth(a, b);
System.out.println("a and b after call: " +a + " " + b);
}
} a and b before call: 15 20
a and b after call: 15 20
call-by- reference
41
• This means that. changes made to the parameter will affect the
argument used to call the subroutine
Java Program for Objects are passed through their references.
42
class Test {
int a, b;
Test(int i, int j)
{
a = i;
b = j;
}
// pass an object
void meth(Test o)
{
o.a *= 2;
o.b /= 2;
}
}
class PassObjRef 43
{
public static void main(String[] args)
{
Test ob = new Test(15, 20);
System.out.println("ob.a and ob.b before call: " + ob.a + " " +ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " + ob.a + " " + ob.b);
}
System.out.println("a, b, and c: " + ob.a + " " + ob.b + " " + ob.getc());
}
}
Understanding static….
• There will be times when you will want to define a class member
53
classname.method()
• Here, classname is the name of the class in which the static method
is declared.
• A static variable can be accessed in the same way by use of the dot
operator on the name of the class.
class StaticDemo
{ 57
void test()
{
Inner inner = new Inner();
inner.display();
}
}
class InnerClassDemo
{ 61