Tuesday, December 5, 2023

Classes, Object and Method



7.I INTRODUCTION

All real world applications involve objects and collaborative operations among those object
For example, if we consider the banking system, the objects of interest include custome.
account, clerk and manager, etc. Any transaction in a bank involves collaborative activiies
of one or more of these objects. Central to the object oriented programming paradigm are
the concepts of classes and objects. Here all the objects in the real world are represented by
objects in the programming environments too. A collection of the objects of similar type is
called class. The classes are the blueprints for the objects and its type. In other words, classes
are the abstractions of objects and the objects are the instances of the classes. Java being
purely an object oriented language supports the concepts of classes and objects very well and
all operations are realized through the use of classes and their objects.
7.2 CLASS DEFINITION INSTANCE VARIABLES
AND MEMBER METHODS
The syntax of defining a class is as follows:
class classname
instance varĂ­ables
member methodsalihe

Here class is the keyword. The classname is the user defined name for the class type and
it should be a valid Java identifier. The instance variables are the member data of the class.
They are declared as
data-type instance-variable;
fomber methods are the methods which will operate on the instance variables of the
objects of the class. The syntax of defining a method is as follows:
return_type method -name (arguments)
statements
where return_type is the type of the value expected to be returned from the method. The
othod name is the name of the method and it should be a valid Java identifier. The arguments
(optional) are the inputs to the method to be passed from the calling method.
73 DECLARATION AND CREATION OF OBJECTS,
ACCESSING MEMBERS
A class represents the blue print of a set of objects with common member data and common
hehaviour. To be able to use the member data and to perform the operations over the member
data, we need to create objects of the class. This has to be done in another method which is
not a member of the class. The non-member method of the class which deals with the obiects
of the class is termed the calling method.
The process of creating an object of type classname is a two-step process:
1. Declaring a reference of type classname
2. Allocating memory for an object of type classname using new operator and assigning
the reference to the object to the reference
The syntax of creating an object of the class type classname 1s as follows:veleelhbs
classname obj;
0bj = new classname();
Or
The declaration of the reference obi and allocation of memory for an object and assigning
die reterence to the object created to the reference obj can be combined together is as follows:
classname obi = new classname();
We use the second version throughout the book.
oCan access the members (member data and member methods) of an object with the
help of an operator called member operator. The symbol for the operator is dot (-). It expects
an object reference to the left of it and a member to the right of it.

ouhlic static vo1d main(String s[l)
Input-Output:
{
Measure m = new Measure():
m.feet = 12;
m.inches = 6.5f;
2
m.display();
12 feet - 6.5 inches
7.4 CLASSIFICATION OF MEMBER METHODS
{
Depending on the data communication between the member methods and the calling methods,
the member methods are classified into four categories:
1 Methods with no arguments and no return value
Methods with arguments and no return value
3. Methods with arguments and with return value
4. Methods with no arguments but return value
Methods with no arguments and no return value
Methods with no arguments and no return value do not take any arguments from the calling
method and do not return any value to the calling method. There is no data communication
between the member method and the calling method.
The general form of the member method is as follows:
void method name ()
statements
Note that the return type is void, which indicates that the member method does not return
any value to the calling method and no arguments are specified within the parentheses atter
Bethod name. Let us now look at a program which illustrates this kind of member methods.
The syntax of invoking the method is as follows:
objref .method name(0;
where objref is a reference to an object of the classtype under consideration; method name is the
name of the method of the class.

4
PROGRAM 7.1 To illustrate method
Input-Output:
6.3
}
import java . util . *;
class Measure
}
class MM1
on the Screen.
int feet;
float inches;
void get ()
Scanner sc = new Scanner (System. in);
feet = sC. nextInt ();
inches = sC.nextFloat ();
void display ()
Enter feet and inches
System. out. println (feet + + inches ) ;
public static void main(String s[])
Measure m = new Measure();
without arguments and
System. out.printIn("Enter feet and inches") ;
m.get ();
System.out. print ("The Measurement is );
m.display(0;
The Measurement is 4-6.3
no return vaue
The class measure is defined with two instance variables feet and inches of type int and
respectively and two member methods get() and display(). The purpose of get () is to accept t
values feet and inches values of an object of type measure and that of display() is to display them on the screen. 

Here it can be noticed that both get() and display() belong to the category of methods
without arguments and no return value as the return type is specified as void for both the
we and no arguments are specified within the parentheses after the method names as
shown below:
void get()
Scanner sc = new Scanner (System. in):
feet = sc. next Int ();
inches = sC.nextFloat ();
void display ()
System.out. println (feet + ""+ inches) :
{
In the main() method of the class MM1 an object of Measure type is created with its reference
in the variable m. The method invocation m.get (); enables us to accept feet and inches values
of the object created and m.display (); displays them on the screen.
Methods with arguments and no return value
Methods with arguments and no return value take arguments from the calling method and do
not return any value to the calling method. There is one way data communication between the
member method and the calling method. That is from calling method to the member method
of the class.
The general form of the member method is as follows:
Classes, Objects and Methods 141
void method name (arguments )
statements
Note that the return type is void and arguments are specified within the parentheses after
method name.
The syntax of invoking the method is as follows: totetesgsiti
objref.method_name (arguments) ;
where objref is a reference to an object of the classtype under consideration; nethod _name is the
name of the method and arguments are the values provided by the calling method.
Let us now look at a program which illustrates this kind of member methods.
PROGRAM 7.2 To illustrate method with arguments but no return valuea
class Measure
{
int feet;

}
}
}
Input-Output:
float inches;
class MM2
void get (int f, float i)
{
feet = f;
void display ()
inches = i;
public static void main(String s[])
System. out.println (feet + " + inches);
Measure m = new
m.get(5, 7.23f);
System.out. print(The Measurement is ");
m. display ();
feet = f;
inches = i;
The Measurement is 5-7.23
The class measure is defined with two instance variables feet and inches of type int and
float respectively and two mnember methods get() and display(). The purpose of get() st
set feet and inches values of an object of type measure and that of display() is to dispia
them on the screen.
10 Here it can be noticed that the method get() belongs to the category of methods
arguments and no return value as the return type is specified as void and arguments are spou
within the parentheses after the method name as shown below:
void get(int f, float i)
in the variable m. The method invocation m.get (5, 7.23f) ; enables us to set the feet and
In the main() method of the class MM2 an object of Measure type is created with h its referen:
inches values of the object created and m.display(); displays them on the screen.


Methods with arguments and with return value
Methods with arguments and with return value take arguments from the calling method and
return a value to the calling method. There is two way data communication between the
member method and the calling method. That is from calling method to the member method
of the class and Vice-versa.
The general form of the member method is as follows:
return_type method_name (arguments )
Statements
Return (expression);
Note that return_type is anything other than void and arguments are specified within the
orentheses after method_name. The statement return (expression ) is responsible for returning a
alne of type return_type to the calling method. The expression should be of type return type
ar should be convertible to return_type. Let us now look at a program which illustrates this
kind of member methods.
The syntax of invoking the method is as follows:
variable = objref. method name (arguments);
where objref is a reference to an object of the classtype under consideration; method name is the
name of the method and arguments are the values provided by the calling method. The variable
is to collect the value returned by the method. The type of variable should be compatible with
the value returned by the method.
PROGRAM 7.3 To illustrate mnethod with arguments and return value
class Measure
int feet;
float inches;
{
float getMeters (int f, float i)sEbsot
feet = f;
Classes, Objects and Methods 143
oos inches = i;
float meters = (feet + inches/12) /3 . 28f;
return meters;
void display ()
System.out. println (feet + "" + inches);

{
}

class MM3
{
Input-Output:
5-6.4
public static void main(String s[])
{
WIth JAVA
Measure m = new Measure();
float meters = m.getMeters (5, 6.4f) ;
The Measurement in feet and inches
System. out. println ("the Measurement in feet and inches");
m.display ();
System. out. println ("The Measurement in
The Measurement in meters = 1.6869919
feet = f;
and float respectively and two member methods getMeters () and display(). The purpOst
getMeters () is to set feet and inches values of an object of type measure with the argumEN
provided and return the meters equivalent of the measurement and that of display() ) is to isja
The class measure is defined with two instance variables
the measurement in feet and inches on the screen.
inches = i;
float getMeters (int f, float i)
meters = + meterS);
Here it can be noticed that the method getMeters() belongs to the category ofm
with arguments and with return value as the return type is specified as float and arguments
specified within the parentheses after the method name as shown below:
float meters = (feet + inches /12) /3.28f;
return meters;
feet and inches
of type i
Methods without arguments but with return value
In the main() method of the class MM3 an object of Measure type is created with its Tetere
in the variable m. The method invocation float meters = m.getMeters (5, 6.4f) ;enables US 0s
the feet and inches values of the object created and the value returned by the method 1S Coue
by the float type variable meters. The method invocation m. display (0; displays the measu
in feet and inches on the screen. The value of meters is also displayed on the soiev
method and but return a value to the calling method. There is one way data Communicatl
Methods without arguments and with return a value to  take arguments from the call


0
nts
between the member method and the calling method. That is from the member method to the
calling mnethod.
The general formn of the member method is as follows:
return type method name()
is
statements
return (expression)
Note that return_type is anything other than void and no arguments are specified within
the parentheses method name. The statement return (expression) is responsible for returning a
value of type reeturn type to the calling method. The expression should be of type return type
or should be convertible to return type. Let us now look at a program which illustrates this
kind of member mnethods.
The syntax of invoking the method is as follows:
variable = objref.method name():
where objref is a reference to an object of the classtype under consideration: the method name
dbe name of the method. The variable is to collect the value returned by the method, The
Classes, Objects and Methods 145
type of variable should be compatible with the value returned by the method.
PROGRAM 7.4 To illustrate method without arguments but with return value
class Measure
int feet;
float inches;
void get(int f, float i)
feet = f;
inches = ij
float getMeters()
float meters = (feet + inches /12) /3.28f;
return meters;
void displav() oaibnnodsoovn bodtern bs bstsg dot i0
System.out. printIn (feet + _" + inches);



No comments:

Post a Comment

Web Technology and Design

                    Internet Principles In 1984 William Gibson in his sci-fi novel Neuromancer coined the term Cyberspace. It refers to the ...