0% found this document useful (0 votes)
6 views

Introducing Classes

Uploaded by

s.hemaswathi
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)
6 views

Introducing Classes

Uploaded by

s.hemaswathi
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/ 12

Introducing Classes

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
}
}
A Simple Class
class Box
{
Class- Variables double width;
double height;
class Box double depth;
{ }
class BoxDemo
double width; {
double height; public static void main(String args[])
{
double depth; Box mybox = new Box();
} double vol;
mybox.width = 10;
Class- Object mybox.height = 20;
mybox.depth = 15;

Box mybox = new Box(); vol = mybox.width * mybox.height *mybox.depth;


System.out.println("Volume is " + vol);
mybox.width=100; }
}
Adding a Method to the Box Class
class Box {
Box mybox1 = new Box();
double width; Box mybox2 = new Box();
double height; // assign values to mybox1's instance
double depth; variables
void volume() mybox1.width = 10;
{ mybox1.height = 20;
mybox1.depth = 15;
System.out.print("Volume is ");
/* assign different values to
System.out.println(width * height * mybox2's instance variables */
depth); mybox2.width = 3;
} mybox2.height = 6;
} mybox2.depth = 9;
class BoxDemo3 { // display volume of first box
public static void main(String args[]) mybox1.volume();
// display volume of second box
{
mybox2.volume();
Output:
}
Volume is 3000.0 }
Volume is 162.0
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, but with no return
type.
• The constructor is automatically called immediately after
the object is created, before the new operator
completes.
• The constructor's job is to initialize the internal state of
an object, ensuring that the code creating an instance will
have a fully initialized, usable object immediately.
class Box { class BoxDemo6
double width; {
double height; public static void main(String args[]) {
// declare, allocate, and initialize Box
double depth;
objects
// This is the constructor for Box.
Box mybox1 = new Box();
Box() { Box mybox2 = new Box();
System.out.println("Constructing Box"); double vol;
width = 10; // get volume of first box
height = 10; vol = mybox1.volume();
depth = 10; System.out.println("Volume is " + vol);
// get volume of second box
}
vol = mybox2.volume();
// compute and return volume System.out.println("Volume is " + vol);
double volume() { }
return width * height * depth; }
Output
} Constructing Box
} Constructing Box
Volume is 1000.0
Volume is 1000.0
Parameterized Constructors
class Box { class BoxDemo7
{
double width;
public static void main(String args[])
double height;
{
double depth; // declare, allocate, and initialize Box objects
// This is the constructor for Box. Box mybox1 = new Box(10, 20, 15);
Box(double w, double h, double d) Box mybox2 = new Box(3, 6, 9);
{ double vol;
width = w; // get volume of first box
vol = mybox1.volume();
height = h;
System.out.println("Volume is " + vol);
depth = d; // get volume of second box
} vol = mybox2.volume();
// compute and return volume System.out.println("Volume is " + vol);
double volume() }
{ }
return width * height * depth;
} Output:
Volume is 3000.0
}
Volume is 162.0
The this Keyword
Sometimes a method will need to refer to the object that
invoked it.
To allow this, Java defines the this keyword. this can be used
inside any method to refer to the current object.
That is, this is always a reference to the object on which the
method was invoked.
You can use this anywhere a reference to an object of the
current class’ type is permitted
// A redundant use of this.
Box(double w, double h, double d)
{
this.width = w;
this.height = h;
this.depth = d;
}
The finalize( ) Method
• Sometimes an object will need to perform some action when
it is destroyed. For example,
• if an object is holding some non-Java resource such as a file
handle or character font, then
• you might want to make sure these resources are freed
before an object is destroyed
protected void finalize( )
{
// finalization code here
}
A STACK CLASS
class Stack {
int stck[] = new int[10];
int tos;
// Initialize top-of-stack
Stack() {
tos = -1;
}
// Push an item onto the stack
void push(int item) {
if(tos==9)
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
// Pop an item from the stack
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
Overloading Methods // Overload test for a double parameter
class OverloadDemo double test(double a)
{ {
void test() System.out.println("double a: " + a);
return a*a;
{
}
System.out.println("No parameters"); }
} class Overload
// Overload test for one integer {
parameter. public static void main(String args[])
void test(int a) {
OverloadDemo ob = new OverloadDemo();
{
double result;
System.out.println("a: " + a); // call all versions of test()
} ob.test();
// Overload test for two integer parameters. ob.test(10);
void test(int a, int b) ob.test(10, 20);
{ result = ob.test(123.25);
System.out.println("a and b: " + a + " " System.out.println("Result of ob.test(123.25):
+ b); " + result);
}
} }
OUTPUT
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625

You might also like