Chapter 8: Classes, Objects and Methods
Anything we wish to represent must be encapsulated in a class that defines the state
and the behaviour of the basic program components known as objects.
Classes create objects and objects use methods to communicate between them.
In Java, the data items are called fields and the functions are called methods.
8.1 Defining a Class
The basic form of a class definition is:
Class classname [extends superclassname]
{
[variable declaration;]
[methods declaration;]
}
Classname and superclassname are valid Java identifiers
The keyword extends indicates that the properties of the superclassname class are
extended to the classname class. This is known as inheritance
8.2 Adding Variables
Data is encapsulated in a class by placing data fields inside the body of the class
definition. These variables are called instance variables because they are created whenever
an object of the class is instantiated.
8.3 Adding Methods
A class with only data fields and without methods that operate on that data has no life.
The objects created by such a class cannot respond to any messages.
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.
The general form of a method declaration is:
Type methodname (parameter-list)
{
Method-body;
}
Method declarations have four basic parts:
1. the name of the method (methodname)
2. the type of the value the method returns (type)
3. A list of parameters (parameter-list)
4. The body of the method
The type specifies the type of value the method would return.
Could be a simple data type such as int as well as any class type.
It could even be void type if the method does not return any value.
The methodname is a valid identifier.
The parameter list is always enclosed in parenthesis.
This list contains variable names and types of all the values we want to give to the
method as input.
The variables in the list are separated by commas
In the case where no input data are required, the declaration must retain the empty
parenthesis.
8.4 Creating Objects
, An object is a block of memory that contains space to store all the instance variables.
Creating an object is also referred to as instantiating an object.
Objects are created using the new operator.
The new operator creates an object of the specified class and returns a reference to that
object.
The first statement declares a variable to hold the object reference and the second one
assigns the object reference to the variable.
8.5 Accessing Class Members
All variables must be assigned values before they are used.
To access the instance variables and the methods directly outside the class, we use the
concerned object and the dot operator.
Objectname. Variablename
Objectname.methodname (parameter-list)
o Objectname is the name of the object, variablename is the name of the instance
variable inside the object that we wish to access, methodname is the method that
we wish to call, and parameter-list is a comma separated list of “actual values” that
must match in type and number with the parameter list of the methodname
declared in the class.
8.6 Constructors
Constructor enables an object to initialize itself when it is created.
Constructors have the same name as the class itself
They do not specify a return type, not even void. This is because they return the
instance of the class itself.
Eg:
Class Rectangle
{
int length;
int width;
Rectangle (int x, int y) // Constructor method
{
Length = x;
Width = y;
}
Int rectArea ()
{
Return (length * width);
}
}
8.7 Methods Overloading
It is possible to create methods that have the same name, but different parameter lists
and different definitions. This is called method overloading.
Is used when objects are required to perform similar tasks but using different input
parameters.
When we call a method in an object, Java matches up the method name first and then
the number and type of parameters to decide which one of the definitions to execute. This
process is called polymorphism.
8.8 Static Members
Static members can be defined as follows:
Anything we wish to represent must be encapsulated in a class that defines the state
and the behaviour of the basic program components known as objects.
Classes create objects and objects use methods to communicate between them.
In Java, the data items are called fields and the functions are called methods.
8.1 Defining a Class
The basic form of a class definition is:
Class classname [extends superclassname]
{
[variable declaration;]
[methods declaration;]
}
Classname and superclassname are valid Java identifiers
The keyword extends indicates that the properties of the superclassname class are
extended to the classname class. This is known as inheritance
8.2 Adding Variables
Data is encapsulated in a class by placing data fields inside the body of the class
definition. These variables are called instance variables because they are created whenever
an object of the class is instantiated.
8.3 Adding Methods
A class with only data fields and without methods that operate on that data has no life.
The objects created by such a class cannot respond to any messages.
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.
The general form of a method declaration is:
Type methodname (parameter-list)
{
Method-body;
}
Method declarations have four basic parts:
1. the name of the method (methodname)
2. the type of the value the method returns (type)
3. A list of parameters (parameter-list)
4. The body of the method
The type specifies the type of value the method would return.
Could be a simple data type such as int as well as any class type.
It could even be void type if the method does not return any value.
The methodname is a valid identifier.
The parameter list is always enclosed in parenthesis.
This list contains variable names and types of all the values we want to give to the
method as input.
The variables in the list are separated by commas
In the case where no input data are required, the declaration must retain the empty
parenthesis.
8.4 Creating Objects
, An object is a block of memory that contains space to store all the instance variables.
Creating an object is also referred to as instantiating an object.
Objects are created using the new operator.
The new operator creates an object of the specified class and returns a reference to that
object.
The first statement declares a variable to hold the object reference and the second one
assigns the object reference to the variable.
8.5 Accessing Class Members
All variables must be assigned values before they are used.
To access the instance variables and the methods directly outside the class, we use the
concerned object and the dot operator.
Objectname. Variablename
Objectname.methodname (parameter-list)
o Objectname is the name of the object, variablename is the name of the instance
variable inside the object that we wish to access, methodname is the method that
we wish to call, and parameter-list is a comma separated list of “actual values” that
must match in type and number with the parameter list of the methodname
declared in the class.
8.6 Constructors
Constructor enables an object to initialize itself when it is created.
Constructors have the same name as the class itself
They do not specify a return type, not even void. This is because they return the
instance of the class itself.
Eg:
Class Rectangle
{
int length;
int width;
Rectangle (int x, int y) // Constructor method
{
Length = x;
Width = y;
}
Int rectArea ()
{
Return (length * width);
}
}
8.7 Methods Overloading
It is possible to create methods that have the same name, but different parameter lists
and different definitions. This is called method overloading.
Is used when objects are required to perform similar tasks but using different input
parameters.
When we call a method in an object, Java matches up the method name first and then
the number and type of parameters to decide which one of the definitions to execute. This
process is called polymorphism.
8.8 Static Members
Static members can be defined as follows: