You are on page 1of 58
Classes, Objects and Methods 7.1 INTRODUCTION All real world applications involve objects and collaborative operations among, those objects For example, if we consider the banking sysiem, the objects of interest include customer, aecount, clerk and manager, etc. Any transaction in a bank involves collaborative activities of one or more of these objects. Cental 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 classnane { instance_variables mmenber_methods Classes, Objects end methods 187 Here class is the keyword. The classnane 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 «lass. ‘They are declared as data-type instance-variable; Member 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-nane(argunents) { statenents. } where return_type is the type of the value expected to be returned from the method. The nethod_nane is the name of the method and it should be a valid Java identifier. ‘The argunents (optional) are the inputs to the method to be passed from the calling method. 7.3. DECLARATION AND CREATION OF OBJECTS, ACCESSING MEMBERS A class represents the blue print of a set of objects with common member data and common behaviour. To be able to use the memher data and to perform the operations aver the member data, we need to create objects of the class. This has to be done in smother method which is not a member of the class. ‘The non-member method of the class which deals with the objects of the class is termed the calling method. The process of creating an object of type classnane is a two-step process: I. Declaring a reference of type classnane 2. Allocating memory for an object of type classnane using new operator and assigning the reference to the object to the reference The syntax of creating an object of the class type classnane is as follows: classname obj; Obj ~ new classname(); or The declaration of the reference obj and allocation of memory for an object and assigning the reference to the object created to the reference obj can be combined together is 2s follows: classname obj = new classnane(); We use the second version throughout the book. We can 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 18B cdject oriented Programming with JAVA Example: class ero { dint eno; string name; void get() 4 eno = 121; } void display() { System.out.printla(eno); system.out.println(nane) ; t } ‘The syntax of declaring and creating an object of emp type is as follows: comp © = new emp(); or ‘emp e = new enp(); e-eno refers to eno value of the object e e.name refers o name of the object e enget()s enables us (0 provide the member data (eno, nane)of e e.display(); enables us to display the member data (eno, rame)of ¢ class Measure { int feet; float inches; void display() { system.out.printIn(feet + “feat -” inches + “inches”; }

You might also like