Software Practice 1 - OOP (1) Class and...
Transcript of Software Practice 1 - OOP (1) Class and...
![Page 1: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/1.jpg)
Software Practice 1 -
OOP (1) – Class and Method
What is OOP?
Defining Classes
Using Classes
References vs. Values
Encapsulate
1
Prof. Hwansoo Han
T.A. Jeonghwan Park 41
T.A. Sung-in Hong 42
![Page 2: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/2.jpg)
Objects in real world
Represent the real world
2
Baby
![Page 3: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/3.jpg)
Objects in real world
Represent the real world
3
Baby
Name
Gender
Weight
Poops
![Page 4: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/4.jpg)
Objects in real world
4
Name
Weight
Gender
…
cry
Baby
![Page 5: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/5.jpg)
Objects in real world
5
Name
Weight
Gender
…
cry
Baby1
Name
Weight
Gender
…
cry
Baby2
Name
Weight
Gender
…
cry
Baby3
More Babies …
![Page 6: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/6.jpg)
Objects in real world
6
Baby1
Baby2
Baby3 Nursery
Babies
Nurses
![Page 7: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/7.jpg)
Objects in real world
7
Baby1
Baby2
Baby3 Nursery
Babies
Nurses
Nurse1
Nurse2
Nurse3
![Page 8: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/8.jpg)
Objects in real world
8
Baby1
Baby2
Baby3
Nursery
Babies
Nurses
Nurse1
Nurse2
Nurse3
ER
… … …
Hospital
![Page 9: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/9.jpg)
Objects in real world
9
Baby1
Baby2
Baby3
Nursery
Babies
Nurses
Nurse1
Nurse2
Nurse3
ER
… … …
Hospital
What do we need to model this objects
with programming language?
![Page 10: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/10.jpg)
Object Oriented Programming
Definition
• A method of programming based on a hierarchy of
classes, and well-defined and cooperating objects.
Class – a structure that defines
• the fields to store the data
• the methods to work on that data
Object – an executable copy of a class
10
![Page 11: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/11.jpg)
Object Oriented Programming
A class
• can be inherited by only one other class
• can implement one or more interfaces
An object
• can establish the relationship with other objects
through the reference
• encapsulates some fields or methods
for hiding them from other objects
11
![Page 12: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/12.jpg)
DEFINING CLASSES
12
![Page 13: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/13.jpg)
Class definition
public class Baby {
String name;
boolean gender;
double weight;
int numPoops = 0;
void poop() {
numPoops += 1;
System.out.println (‚Dear mother, ‚ +
‚I have pooped. Ready the diaper.‛);
}
}
13
![Page 14: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/14.jpg)
Let’s declare a baby!
public class Baby {
}
14
Fields
Methods
![Page 15: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/15.jpg)
Baby fields
public class Baby {
TYPE var_name;
TYPE var_name = some_value;
}
15
![Page 16: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/16.jpg)
Baby fields
public class Baby {
String name;
boolean gender;
double weight = 5.0;
int numPoops = 0;
}
16
![Page 17: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/17.jpg)
Baby Siblings?
public class Baby {
String name;
boolean gender;
double weight = 5.0;
int numPoops = 0;
XXXXX YYYYY;
}
17
![Page 18: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/18.jpg)
Baby Siblings?
public class Baby {
String name;
boolean gender;
double weight = 5.0;
int numPoops = 0;
Baby[] siblings;
}
18
![Page 19: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/19.jpg)
Baby methods
public class Baby {
String name = ‚Slim Shady‛;
...
void sayHi() {
System.out.println (
‚Hi, my name is.. ‚ + name);
}
}
19
![Page 20: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/20.jpg)
Baby methods
public class Baby {
double weight = 5.0;
...
void eat(double foodWeight) {
if (foodWeight >= 0 &&
foodWeight < weight) {
weight += foodWeight;
}
}
20
![Page 21: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/21.jpg)
Baby class
public class Baby {
String name;
boolean gender;
double weight;
int numPoops = 0;
Baby[] siblings;
void sayHi() {...}
void eat(double foodWeight) {...}
}
21
![Page 22: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/22.jpg)
Ok, let’s make this baby!
Baby myBaby = new Baby();
22
But what about his/her name? gender?
![Page 23: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/23.jpg)
Constructors
public class CLASSNAME {
CLASSNAME ( ) {
}
CLASSNAME ( [ARGUMENTS] ) {
}
}
CLASSNAME obj1 = new CLASSNAME();
CLASSNAME obj2 = new CLASSNAME([ARGUMENTS]);
23
![Page 24: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/24.jpg)
Constructors
Constructor name == the class name
No return type – never returns anything
Usually initialize fields
All classes need at least one constructor
• If you don‟t write one, defaults to
24
CLASSNAME () {
}
![Page 25: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/25.jpg)
Baby constructor
public class Baby {
String name;
boolean gender;
Baby(String myName, boolean myGender) {
name = myName;
gender = myGender;
}
}
25
![Page 26: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/26.jpg)
Final Baby class
public class Baby {
String name;
boolean gender;
double weight;
int numPoops = 0;
Baby[] siblings;
Baby() {...}
void sayHi() {...}
void eat(double foodWeight) {...}
}
26
![Page 27: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/27.jpg)
USING CLASSES
27
![Page 28: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/28.jpg)
Classes and Instances
// class Definition
public class Baby { … }
// class Instances
Baby shiloh = new Baby (‚Shilo Jolie-Pitt‛, true);
Baby knox = new Baby (‚Knox Jolie-Pitt‛, true);
28
![Page 29: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/29.jpg)
Accessing fields
Object.FIELDNAME
Baby shiloh = new Baby (‚Shiloh Jolie-Pitt‛, true);
System.out.println (shiloh.name);
System.out.println (shiloh.numPoops);
29
![Page 30: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/30.jpg)
Accessing fields
Object.METHODNAME([ARGUMENTS])
Baby shiloh = new Baby (‚Shiloh Jolie-Pitt‛, true);
shiloh.sayHi(); // ‚Hi, my name is.. Shiloh Jolie-Pitt‛
shiloh.eat(1);
30
![Page 31: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/31.jpg)
REFERENCES VS. VALUES
31
![Page 32: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/32.jpg)
Primitives vs. References
Primitive types are basic java types
• int, long, double, boolean, char, short, byte, float
• The actual values are stored in the variable
Reference types are arrays and objects
• String, int[], Baby, …
32
![Page 33: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/33.jpg)
How java stores primitives
Variables are like fixed size cups
Primitives are small enough that they just
fit into the cup
33
int double char boolean
![Page 34: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/34.jpg)
How java stores objects
Objects are too big to fit in a variable
• Stored somewhere else
• Variable stores an address that locates the object
34
Object
![Page 35: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/35.jpg)
How java stores objects
Objects are too big to fit in a variable
• Stored somewhere else
• Variable stores an address that locates the object
35
Object‟s
location Object Object Object
Object Object Object
![Page 36: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/36.jpg)
References
The object’s location is called a reference
== compares the references
Baby shiloh1 = new Baby(‚Shiloh‛);
Baby shiloh2 = new Baby(‚Shiloh‛);
Does shiloh1 == shiloh2?
36
![Page 37: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/37.jpg)
References
The object’s location is called a reference
== compares the references
Baby shiloh1 = new Baby(‚Shiloh‛);
Baby shiloh2 = new Baby(‚Shiloh‛);
Does shiloh1 == shiloh2?
37
NO!
![Page 38: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/38.jpg)
References
Baby shiloh1 = new Baby(‚Shiloh‛);
Baby shiloh2 = new Baby(‚Shiloh‛);
38
reference reference
shiloh1 shiloh2
Name=“Shiloh”
Name=“Shiloh”
![Page 39: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/39.jpg)
References
Baby shiloh1 = new Baby(‚Shiloh‛);
Baby shiloh2 = new Baby(‚Shiloh‛);
39
reference reference
shiloh1 shiloh2
Name=“Shiloh”
Name=“Shiloh”
≠
![Page 40: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/40.jpg)
Relations between objects
1. using ==
• shiloh1 == shiloh2
• Check two variables reference exactly same
2. using field
• shiloh1.name == shiloh2.name
• Compare a field of the object
3. using user-defined method
• shiloh1.equals (shiloh2)
• Used when the objects are different, but determine the
same or not by the fields stored in the object
40
![Page 41: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/41.jpg)
References
Using = updates the reference
baby1 = baby2; //?
41
baby1
location
baby1
baby2
location
baby2
baby1
object
baby2
object
![Page 42: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/42.jpg)
References
Using = updates the reference
baby1 = baby2;
42
baby1
baby2
location
baby2
baby1
object
baby2
object
![Page 43: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/43.jpg)
References
Baby myBaby = new Baby(‚Davy‛, true);
43
myBaby„s
location
name = “Davy”
gender = true
![Page 44: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/44.jpg)
References
Baby myBaby = new Baby(‚Davy‛, true);
myBaby.name = ‚David‛;
44
myBaby„s
location
name = “Davy”
gender = true
![Page 45: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/45.jpg)
References
Baby myBaby = new Baby(‚Davy‛, true);
myBaby.name = ‚David‛;
45
myBaby„s
location
name = “David”
gender = true
![Page 46: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/46.jpg)
References
Using [ ] or .
• Follows the reference to the object
• May modify the object, but never the reference
Imagine
• Following directions to a house
• Moving the furniture around
Analogous to
• Following the reference to an object
• Changing fields in the object
46
![Page 47: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/47.jpg)
Self reference
Java class has a special way to access itself
class Baby {
String[] words;
…
void remember (String[] words) {
String word;
for (word : words) words[top++] = word; // ???????
}
}
47
![Page 48: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/48.jpg)
Self reference
Java class has a special way to access itself
class Baby {
String[] words;
…
void remember (String[] words) {
String word;
for (word : words) this.words[top++] = word; // !
}
}
48
![Page 49: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/49.jpg)
Methods and references
void doSomething(int x, int[] ys, Baby b) {
x = 99;
ys[0] = 99;
b.name = ‚99‛;
}
…
int i = 0;
int[] j = { 0 };
Baby k = new Baby(‚50‛, true);
doSomething (i, j, k);
i = ? j[0] = ? k.name = ?
49
![Page 50: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/50.jpg)
ENCAPSULATE
50
![Page 51: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/51.jpg)
Encapsulation
In real world, there are huge # of objects
and all of them have privacy.
Electric objects, too!
51
![Page 52: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/52.jpg)
Public and Private
public
• Able to access/modify it whoever having the object
public class Baby {
public String nickname;
}
public class Stranger {
void makeNicknameOf (Baby b) {
b.nickname = ‚cuty‛;
}
}
52
![Page 53: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/53.jpg)
Public and Private
private
• Only the object itself can access it
public class Baby {
private String nickname;
}
public class Stranger {
void makeNicknameOf (Baby b) {
b.nickname = ‚cuty‛; // Error!
}
}
53
![Page 54: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/54.jpg)
How to access private data?
public class Baby {
private String nickname;
public void setNickname (String nickname, Object o) {
// check if the object is instance of [Stranger] or not
if (o instanceOf Stranger) {
return;
}
this.nickname = nickname;
}
}
public class Stranger {
void makeNicknameOf (Baby b) {
b.setNickname (‚cuty‛, this);
}
}
54
![Page 55: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/55.jpg)
Then, what is String in java?
Built-in class for handling the sequence of characters in high level abstraction
Usage
String name = ‚Simon‛;
char[] data = {‘s’, ‘i’, ‘n’, ‘g’, ‘l’, ‘e’};
String state = new String (data);
// String concatenate
System.out.println (name + ‚ is a ‛ + state); // Simon is single
// access the substring with the range of indexes
System.out.println (state.substring (1)); // ingle
System.out.println (state.substring (2,4)); // ngl
55
![Page 56: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/56.jpg)
Then, what is String in java?
Built-in class for handling the sequence of characters in high level abstraction
Usage
String name = ‚Simon‛;
char[] data = {‘s’, ‘i’, ‘n’, ‘g’, ‘l’, ‘e’};
String state = new String (data);
// comparison
System.out.println (name == ‚Simon‛); // false
System.out.println (name.compareTo (‚Simon‛)); // true
// get length of a string
System.out.println (name.length ()); // 5
56
![Page 57: Software Practice 1 - OOP (1) Class and Methodarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/03_OOP... · 2017-03-27 · Software Practice 1 - OOP (1) – Class and Method What](https://reader033.fdocuments.net/reader033/viewer/2022060406/5f0f5b787e708231d443c228/html5/thumbnails/57.jpg)
[Lab – Practice #2]
Modeling Book and Libraries
• class Book {}
• class Library {}
Books can be
• borrowed
• returned
Library
• keeps track of books
• Hint: use Book[]
57