sahar mosleh california state university san marcospage 1 classes and objects and methods
TRANSCRIPT
![Page 1: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/1.jpg)
Sahar Mosleh California State University San Marcos Page 1
Classes and Objects and Methods
![Page 2: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/2.jpg)
Sahar Mosleh California State University San Marcos Page 2
Class Fundamentals
• A class is a template that defines the form of an object
• It includes both the attributes (data) and a set of methods (routines and code) that operate on the data
• Methods and attributes that constitute a class are called members
• A class specification is used to construct objects . Objects are instances of a class
• You may think of a class as a type, and object as variables instantiated with a specific type (class)
![Page 3: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/3.jpg)
Sahar Mosleh California State University San Marcos Page 3
• The general form of a class is:
class className{
type var1;type var2;…type varN;//--------------------------------type method1 (parameters){
Body of the method}.…type methodN (parameters){
Body of the method}
}
![Page 4: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/4.jpg)
Sahar Mosleh California State University San Marcos Page 4
• So far the only type of class we have seen is the one that contains main() routine
• Note that, the general form of a class does not specify a main() method
• A main() method is required only if that class is the starting point for your program
• Eventually, when you learn about APPLETS in Java, you will notice that applets is Java do not require main() routine
![Page 5: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/5.jpg)
Sahar Mosleh California State University San Marcos Page 5
• Consider the following example:
class Vehicle{
int passenger; // number of passengersint fuelcap // Fuel capacity in gallonsint mpg; // fuel consumption in miles per
gallon}
• This is an example of a simple class.
• The above class definition creates a new data type but nothing is allocated in memory yet.
• So, the class declaration is only a type description, it does not create an actual object
![Page 6: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/6.jpg)
Sahar Mosleh California State University San Marcos Page 6
Creating Objects
• To create a vehicle object, you will use a statement like the following:
Vehicle minivan = new Vehicle();
• At this stage, minivan will be an instance of vehicle and has location in the memory;
• Each object that is instantiated from the vehicle class has its own copy of data members (passengers, fuelcap, mpg)
![Page 7: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/7.jpg)
Sahar Mosleh California State University San Marcos Page 7
Accessing Data Members
• The general form of accessing a member of an object is object.member
• For example, to set the fuelcap of minivan to 16 we do:minivan.fuelcap = 16;
• In general you can use the dot operator to access both instance variables and methods. We will talk about methods shortly.
![Page 8: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/8.jpg)
Sahar Mosleh California State University San Marcos Page 8
• Consider the following example:
class VehicleDemo{public static void main(String args[])
{ Vehicle minivan = new Vehicle();
int range;
minivan.passengers = 7; minivan.fuelcap = 16;minivan.mpg = 21;
range = minivan.fuelcap * minivan.mpg;System.out.println(“Minivan can carry ” + minivan.passengers + “ with a range of ” + range);
}}
class Vehicle{ int passengers;
int fuelcap;int mpg;
}
![Page 9: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/9.jpg)
Sahar Mosleh California State University San Marcos Page 9
Some points to remember about this example
• First, you should call this file VehicleDemo.java because the main() routine is in the VehicleDemo class and not in Vehicle class
• After you compile this program, you will notice that you have two names with extension class in your directory. They are:
Vehicle.class and VehicleDemo.class
• Although in this example both classes, Vehicle and VehicleDemo are in the same source file, it does not have to be this way.
• You may place these two classes in two separate source files. If you compile VehicleDemo.java, the other class will automatically be complied
![Page 10: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/10.jpg)
Sahar Mosleh California State University San Marcos Page 10
How objects are created
• Remember that we mentioned, in order to create an object of type Vehicle, we do the following:
Vehicle minivan = new Vehicle();
• This declaration does two things at the same time:• It declares a variable called minivan of class type Vehicle• Second, the new operator creates a physical copy of object
and assign a reference to that object.
• The above two steps can be written for the minivan object as follows:
Step1 : Vehicle minivan;Step2: minivan = new Vehicle();
• In step one, the value of minivan is assigned NULL automatically by Java
![Page 11: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/11.jpg)
Sahar Mosleh California State University San Marcos Page 11
Reference Variables and Assignments• In an assignment operation, object reference variables act
differently than the variables of a simple type such as int.
• For example, int x, y;x = 5;y = x;
• In this case, x and y refer to different locations in memory but their contents (values) are the same. Thus, if we change x to 6 y still remains 5.
• Now consider, Vehicle car1 = new Vehicle;Vehicle car 2 = car1;
• In this case, both car1 and car2 refer to the same memory location. If the fuelcap attribute of car1 changes, the fuelcap of car2 has changed and vice versa.
![Page 12: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/12.jpg)
Sahar Mosleh California State University San Marcos Page 12
Methods
• So far the classes we have introduced did not contain methods (except the main routine , main())
• Methods are routines with a set of operations that change the state (values) of the attributes (data members)
• The general form of a method is:
returnType methodName(parameter list){
// body of the method}
• Where the return type is the type returned from the method.• methodName is the name of the routine (method) and • parameterList is a sequence of types and identifiers separated
by commas
![Page 13: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/13.jpg)
Sahar Mosleh California State University San Marcos Page 13
Adding method to our Vehicle classclass Vehicle{ int passengers; int fuelcap; int mpg;
void range() { System.out.println(“Range is ” + fuelcap*mpg);}
}//----------------------------class AddMeth{public static void main(String args[])
{ Vehicle minivan = new Vehicle();
Vehicle sportscar = new Vehicle(); int range1, range2; minivan.passengers = 7; sportscar.passengers = 2; minivan.fuelcap = 16; sportscar.fuelcap = 14; minivan.mpg = 21; sportscar.mpg = 12;
System.out.println(“Minivan can carry ”+minivan.passengers + “. ”); minivan.range();
System.out.println(“Sportscar carry ” + sportscar.passengers + “. ”); sportscar.range();}
}
![Page 14: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/14.jpg)
Sahar Mosleh California State University San Marcos Page 14
• In the previous example, the first line of range() is void range()
• That declares a method called range(). This method has no parameters and return type void (i.e. nothing is returned)
• The body of the range method is:System.out.println(“Range is: ” + fuelcap*mpg)
• The range method ends when its closing brace is encountered. This causes the control to be transferred back to the caller of the function
![Page 15: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/15.jpg)
Sahar Mosleh California State University San Marcos Page 15
• In the example, the range method is called as follows:minivan.range()
• This calls the range method on minivan object
• The call to minivan.range() displays the range of vehicle defined by minivan.
• Similarly, sportscar.range() displays the range of vehicle defined by sportscar
• So each time range() is called, it displays the range for specific objects
• But, note that the data fuelcap, and mpg in the range() method are called directly (i.e. without using the dot (.) operator and object name). Why?
• The answer is, a method is always invoked relative to some object of its class. Once this invocation has occurred, the object in known. There is no reason to specify the object name again.
![Page 16: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/16.jpg)
Sahar Mosleh California State University San Marcos Page 16
Returning a Method
• A method terminates under two conditions:• if it completes and closing brace } is encountered• if “return” statement is executed somewhere in the middle
of the method
• If the return type of a method is void, simple return statement:return;
returns the method to the caller
• Otherwise for the none-void methods return statements returns a value to the caller:
return value;
![Page 17: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/17.jpg)
Sahar Mosleh California State University San Marcos Page 17
Example of void method
void myMeth( ){
int i;for (i=0; i<10; i++){
if (i==5) return;System.out.println();
}}
• This is an example of a void method. The method does not return any value at all. It only prints something and terminates
• When the value of “i” becomes 5, the loop terminates and the control goes back to the caller of the method
![Page 18: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/18.jpg)
Sahar Mosleh California State University San Marcos Page 18
class Vehicle{ int passengers; int fuelcap; int mpg;
int range() { return (fuelcap*mpg); }}//----------------------------class RetMeth{public static void main (String args[])
{ Vehicle minivan = new Vehicle(); Vehicle sportscar = new Vehicle(); int range1, range2;
minivan.passengers = 7; sportscar.passengers = 2; minivan.fuelcap = 16; sportscar.fuelcap = 14; minivan.mpg = 21; sportscar.mpg = 12; range1 = minivan.range(); range2 = sportscar.range(); System.out.println(“Minivan can carry ”+minivan.passengers + “ with range of .” + range1 + “ Miles”); System.out.println(“Sportscar can carry ” + sportscar.passengers + “with range of ” + range2 + “ Miles”);}
}
![Page 19: Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods](https://reader036.vdocuments.mx/reader036/viewer/2022083009/5697c0201a28abf838cd275d/html5/thumbnails/19.jpg)
Sahar Mosleh California State University San Marcos Page 19
• In the previous example, notice that range() has a return type of int.
• It means, it returns an integer value to the caller
• The type of the data returned by a method must be compatible with the return type specified by the method
• When a particular non-void function is called, the result of the function can be used in three ways:
• It can be stored. For example:int result = minivan.range();
• It can be printed directly. For example, System.out.println(“The range is ” + minivan.range());
• It can be used in an expression:int y, x, zy = x +z + minivan.range();Orif (minivan.range() > 50)
….….