2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH...
Transcript of 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH...
![Page 1: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/1.jpg)
COMP 110/L Lecture 7
Mahdi Ebrahimi
Some slides adapted from Dr. Kyle Dewey
![Page 2: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/2.jpg)
Outline
• Introduction to objects
• Constructors and new
• Instance variables
• Instance methods
• static vs. non-static
![Page 3: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/3.jpg)
Object-Oriented Programming
![Page 4: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/4.jpg)
Example
Class: Human Object: Man, Woman, Child
Class Fruit Object: Apple, Banana, Mango
Class: Mobile Phone Object: iPhone, X Samsung S10
Class: Food Object: Pizza, Burger, Rice
![Page 5: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/5.jpg)
What is an Object?
![Page 6: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/6.jpg)
Real-world objects has 2 characteristics
1 - State 2 - Behavior
NameColorBreed
barkingwagging tail
Variables (Fields) Methods (Functions)
![Page 7: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/7.jpg)
What is Class?A class is the blueprint which individual objects are created.
Blueprint or template ?
objects i.e. instance of class (blueprint/template/structure/design)
![Page 8: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/8.jpg)
In real-world car is an object and will have 2 characteristics.
1 - State 2 - Behavior
SizeColorMakeModel
MoveAccelerateTurnReverseShift
Variables (Fields) Methods (Functions)
![Page 9: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/9.jpg)
In Programming
Variables (state)
Methods (behavior)
![Page 10: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/10.jpg)
Example 2
![Page 11: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/11.jpg)
Basic IdeaThe world is composed of objects
which interact with each other in well-defined ways
![Page 12: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/12.jpg)
Basic IdeaThe world is composed of objects
which interact with each other in well-defined ways
Example: boiling water
![Page 13: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/13.jpg)
Basic IdeaThe world is composed of objects
which interact with each other in well-defined ways
Example: boiling water
faucet object
![Page 14: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/14.jpg)
Basic IdeaThe world is composed of objects
which interact with each other in well-defined ways
Example: boiling water
faucet object pot object
![Page 15: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/15.jpg)
Basic Idea
faucet object pot object
The world is composed of objectswhich interact with each other in well-defined ways
Example: boiling water
Interaction: fill with water
![Page 16: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/16.jpg)
Basic Idea
pot objectfaucet object
The world is composed of objectswhich interact with each other in well-defined ways
Example: boiling water
Interaction: fill with water
![Page 17: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/17.jpg)
Basic IdeaThe world is composed of objects
which interact with each other in well-defined ways
Example: boiling water
pot object
![Page 18: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/18.jpg)
Basic Idea
pot object
stove object
The world is composed of objectswhich interact with each other in well-defined ways
Example: boiling water
Interaction:Place on top of
![Page 19: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/19.jpg)
Basic IdeaThe world is composed of objects
which interact with each other in well-defined ways
Example: boiling water
stove object
![Page 20: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/20.jpg)
Basic IdeaThe world is composed of objects
which interact with each other in well-defined ways
Example: boiling water
Interaction:Turn on burner
stove object
![Page 21: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/21.jpg)
Creating ObjectsIn Java, we first need a class to make an object.
A class serves as a blueprint/template for an object.
![Page 22: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/22.jpg)
Creating ObjectsIn Java, we first need a class to make an object.
A class serves as a blueprint/template for an object.
Stove Class
![Page 23: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/23.jpg)
Creating ObjectsIn Java, we first need a class to make an object.
A class serves as a blueprint/template for an object.
Stove Class
Stove object Stove object Stove object-The same class can be used to make different stoves-These stoves can be different from each other, perhaps even radically different. It all depends on exactly how the class is defined.
![Page 24: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/24.jpg)
public classDeclares a class, and gives it
public visibility (more on that later in the course)
![Page 25: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/25.jpg)
public classDeclares a class, and gives it
public visibility (more on that later in the course)
public class Table {...
}
![Page 26: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/26.jpg)
Constructors
![Page 27: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/27.jpg)
Constructors• Code executed upon object creation
• Effectively create the object
• Looks like a method, but no return type (not even void) and has the same name as the class
![Page 28: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/28.jpg)
Constructors• Code executed upon object creation
• Effectively create the object
• Looks like a method, but no return type (not even void) and has the same name as the class
public class Table { public Table() {System.out.println( “Creating table...”);
}}
![Page 29: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/29.jpg)
Constructors• Code executed upon object creation
• Effectively create the object
• Looks like a method, but no return type (not even void) and has the same name as the class
Constructorpublic class Table { public Table() {System.out.println( “Creating table...”);
}}
![Page 30: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/30.jpg)
Executing Constructorsnew executes a given constructor,
creating a new object in the process.
![Page 31: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/31.jpg)
Executing Constructorsnew executes a given constructor,
creating a new object in the process.
Table t = new Table();
![Page 32: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/32.jpg)
Example:Table.java
![Page 33: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/33.jpg)
Constructor ParametersJust like methods, constructors can take parameters
![Page 34: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/34.jpg)
Constructor ParametersJust like methods, constructors can take parameters
public class ConsParam {public ConsParam(String str) { System.out.println(str);
}}
![Page 35: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/35.jpg)
Constructor ParametersJust like methods, constructors can take parameters
public class ConsParam {public ConsParam(String str) { System.out.println(str);
}}
ConsParam p = new ConsParam(“hi”);
![Page 36: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/36.jpg)
Example:ConsParam.java
![Page 37: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/37.jpg)
InstanceVariables
![Page 38: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/38.jpg)
InstanceVariablesDeclared in the class.
Each object created from a class (hereafter referred to as an instance) has its own instance variables.
![Page 39: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/39.jpg)
InstanceVariablesDeclared in the class.
Each object created from a class (hereafter referred to as an instance) has its own instance variables.
publicint
classmyInt;
HasInstance// instance
{variable
...}
![Page 40: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/40.jpg)
InstanceVariablesDeclared in the class.
Each object created from a class (hereafter referred to as an instance) has its own instance variables.
public class HasInstance {int myInt; // instance variable public HasInstance(int setInt) {myInt = setInt;
}}
![Page 41: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/41.jpg)
public class HasInstance {int myInt; // instance variable public HasInstance(int setInt) {myInt = setInt;
}}
![Page 42: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/42.jpg)
public class HasInstance {int myInt; // instance variable public HasInstance(int setInt) {myInt = setInt;
}}
HasInstance a = new HasInstance(7);
![Page 43: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/43.jpg)
public class HasInstance {int myInt; // instance variable public HasInstance(int setInt) {
myInt = setInt;}
}
HasInstance a = new HasInstance(7);HasInstance b = new HasInstance(8);
![Page 44: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/44.jpg)
public class HasInstance {int myInt; // instance variable public HasInstance(int setInt) {
myInt = setInt;}
}
HasInstance a = new HasInstance(7);HasInstance b = new HasInstance(8);
HasInstance
myInt: 7
a:
![Page 45: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/45.jpg)
public class HasInstance {int myInt; // instance variable public HasInstance(int setInt) {
myInt = setInt;}
}
HasInstance a = new HasInstance(7);HasInstance b = new HasInstance(8);
HasInstance
myInt: 7
a: HasInstance b:
myInt: 8
![Page 46: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/46.jpg)
Example:HasInstance.java
![Page 47: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/47.jpg)
Instance Methods
![Page 48: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/48.jpg)
Instance Methods
• Define which interactions can occur between objects
• Declared in the class
• Specific to objects created from the class (instances), and operate over instance variables.
![Page 49: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/49.jpg)
public class HasInstance {int myInt; // instance variable public HasInstance(int setInt) {myInt = setInt;
}}
-To show an example, let’s take the HasInstance definition from before...
![Page 50: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/50.jpg)
public class HasInstance2 {int myInt; // instance variable public HasInstance2(int setInt) {myInt = setInt;
}
public void printInt() { System.out.println(myInt);
}}
-...and now we add the printInt instance method-The name of the class has also been changed, just so we can have both examples in two separate files (namely HasInstance.java and HasInstance2.java)
![Page 51: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/51.jpg)
Example:HasInstance2.java
![Page 52: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/52.jpg)
staticAssociates something with the class itself,
as opposed to individual objects created from the class.
![Page 53: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/53.jpg)
staticAssociates something with the class itself,
as opposed to individual objects created from the class.
public class MyClass { public static void main(String[] args) {...
}}
-You’ve been defining main and all your methods this way the entire time-Java forces all source code to be in classes, so this is unavoidable. However, we haven’t really gotten into proper objects yet.
![Page 54: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/54.jpg)
static vs. non-staticWith static: associated with the class.
Without static: associated with objectscreated from the class.
![Page 55: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/55.jpg)
static vs. non-staticWith static: associated with the class.
Without static: associated with objectscreated from the class.
public class MyClass { public static void main(String[] args) {...
}}
![Page 56: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/56.jpg)
static vs. non-staticWith static: associated with the class.
Without static: associated with objectscreated from the class.
public class MyClass { public static void main(String[] args) {...
}}
With classMyClass
![Page 57: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/57.jpg)
static vs. non-staticWith static: associated with the class.
Without static: associated with objectscreated from the class.
public class MyClass { public static void main(String[] args) {...
}}
public class MyClassTest { @Testpublic void someTest() {...}
}
With classMyClass
![Page 58: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/58.jpg)
static vs. non-staticWith static: associated with the class.
Without static: associated with objectscreated from the class.
public class MyClass { public static void main(String[] args) {...
}}
With objects created from MyClassTest public class MyClassTest {@Testpublic void someTest() {...}
}
With classMyClass
![Page 59: 2XWOLQH - GitHub Pages...SXEOLF FODVV +DV,QVWDQFH ^ LQW P\,QW LQVWDQFH YDULDEOH SXEOLF +DV,QVWDQFH LQW VHW,QW ^ P\,QW VHW,QW ` SXEOLF YRLG SULQW,QW ^ 6\VWHP RXW SULQWOQ P\,QW ` ` …](https://reader035.fdocuments.us/reader035/viewer/2022071419/61180c1aee92f61348461eb5/html5/thumbnails/59.jpg)
Stove Example in Java
•Water.java
•Faucet.java
•Pot.java
•Stove.java
•BoilingWater.java