Unit II
Classes, Objects and Methods : Defining a Class – Creating Objects – Accessing
class members – Constructors – Method Overloading – Static Members –
Inheritance – Extending a Class – Overriding Methods – Final variables and
methods – Final Classes – Finalizer methods – Abstract Methods and Classes –
Methods with Varargs – Visibility Control – Arrays, Strings and Vectors – Onedimensional Array –
Creating an Array – Two-dimensional Arrays – Strings –
Vectors – Wrapper Classes – *Enumerated Types*.
Classes, Objects and Methods
✓ Class is a collection of data(variables) and methods that operate on that data
✓ A class is a user defined data type with a template that serves to define its properties.
✓ Class is a template for an object and an Object is an instance of a class
✓
Defining a Class
class classname
{
[ Fields declaration;]
[ methods declaration;]
}
1.Fields Declaration
✓ Data is encapsulated in a class by placing data fields inside the body of the class
definition. These variable called instance variables, because they are created when the
object of the class is instantiated
class Rectangle
{
int length;
int width;
}
1
, ✓ The class Rectangle contains two integer type instance variables.
✓ It is allowed to declare them in one line int length, width
These variables only declared and therefore no storage space has been created in the memory.
2.Methods Declaration
✓ A method is a series of statements that carry out a task
✓ A class with only data fields(and without methods that operate on that data) has no
life. We must therefore add methods that are necessary for manipulating the data
contained in the class. Methods are declared inside the body of the class , but
immediately after the declaration of instance variables.
Syntax:
Type method name ( parameter – list)
{
method- body;
}
Method declaration must include:
1.A declaration (The name of the Method.)
2.The type of the value the method returns. (type)
3.A list of parameters (parameters-list)
4.The body of the method
Example:
class Rectangle
{
int length;
int width;
void getData(int x, int y)
{
length = x;
width=y;
}
int rectArea()
{
int area=length * width;
return(area);
}
}
2
, Creating Objects
✓ An object is a particular instance of a class.
✓ An object in Java is essentially a block of memory that contains space to store all
instance variables.
✓ Creating an object is also referred to as instantiating an object.
✓ Objects in Java are created using the new operator.
✓ The new operator created an object of the specified class and returns a reference to
that object.
Here is an example of creating an object of type Rectangle.
rectangle rect;
rect = new rectangle ();
or
rectangle rect = new rectangle ();
We can create any number of objects for a class.
Eexample:
rectangle rect1 = new rectangle ();
rectangle rect2 = new rectangle ();
Accessing Class Members
✓ Object members (fields and methods) are accessed with a dot between the object
name and the member name.
✓
Objectname.varibale name;
Objectname.methodname (parameter – list)
Example , if we take the Rectangle class
rect1.length=15;
rect1.width=10;
The same way methods also called through object;
Rectangle rect1=new Rectangle() // creating an object
rect1.getData(15,10);
Applications of Classes and Objects
Example:
class Rectangle
{
int length;
int width;
void getData(int x, int y)
3
Classes, Objects and Methods : Defining a Class – Creating Objects – Accessing
class members – Constructors – Method Overloading – Static Members –
Inheritance – Extending a Class – Overriding Methods – Final variables and
methods – Final Classes – Finalizer methods – Abstract Methods and Classes –
Methods with Varargs – Visibility Control – Arrays, Strings and Vectors – Onedimensional Array –
Creating an Array – Two-dimensional Arrays – Strings –
Vectors – Wrapper Classes – *Enumerated Types*.
Classes, Objects and Methods
✓ Class is a collection of data(variables) and methods that operate on that data
✓ A class is a user defined data type with a template that serves to define its properties.
✓ Class is a template for an object and an Object is an instance of a class
✓
Defining a Class
class classname
{
[ Fields declaration;]
[ methods declaration;]
}
1.Fields Declaration
✓ Data is encapsulated in a class by placing data fields inside the body of the class
definition. These variable called instance variables, because they are created when the
object of the class is instantiated
class Rectangle
{
int length;
int width;
}
1
, ✓ The class Rectangle contains two integer type instance variables.
✓ It is allowed to declare them in one line int length, width
These variables only declared and therefore no storage space has been created in the memory.
2.Methods Declaration
✓ A method is a series of statements that carry out a task
✓ A class with only data fields(and without methods that operate on that data) has no
life. We must therefore add methods that are necessary for manipulating the data
contained in the class. Methods are declared inside the body of the class , but
immediately after the declaration of instance variables.
Syntax:
Type method name ( parameter – list)
{
method- body;
}
Method declaration must include:
1.A declaration (The name of the Method.)
2.The type of the value the method returns. (type)
3.A list of parameters (parameters-list)
4.The body of the method
Example:
class Rectangle
{
int length;
int width;
void getData(int x, int y)
{
length = x;
width=y;
}
int rectArea()
{
int area=length * width;
return(area);
}
}
2
, Creating Objects
✓ An object is a particular instance of a class.
✓ An object in Java is essentially a block of memory that contains space to store all
instance variables.
✓ Creating an object is also referred to as instantiating an object.
✓ Objects in Java are created using the new operator.
✓ The new operator created an object of the specified class and returns a reference to
that object.
Here is an example of creating an object of type Rectangle.
rectangle rect;
rect = new rectangle ();
or
rectangle rect = new rectangle ();
We can create any number of objects for a class.
Eexample:
rectangle rect1 = new rectangle ();
rectangle rect2 = new rectangle ();
Accessing Class Members
✓ Object members (fields and methods) are accessed with a dot between the object
name and the member name.
✓
Objectname.varibale name;
Objectname.methodname (parameter – list)
Example , if we take the Rectangle class
rect1.length=15;
rect1.width=10;
The same way methods also called through object;
Rectangle rect1=new Rectangle() // creating an object
rect1.getData(15,10);
Applications of Classes and Objects
Example:
class Rectangle
{
int length;
int width;
void getData(int x, int y)
3