CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Basic Java 03 Objects n Classes
-
Upload
nguyen-minh-tuan -
Category
Documents
-
view
245 -
download
0
description
Transcript of Basic Java 03 Objects n Classes
![Page 1: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/1.jpg)
Objects and Classes
Object-Oriented Programming
![Page 2: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/2.jpg)
Outline
� Classes vs. objects
� Designing a class
� Methods and instance variables
� Encapsulation & information hiding
Đại học Công nghệ - ĐHQG HN Objects and Classes 2
� Encapsulation & information hiding
� Readings:
� HFJ: Ch. 2, 3, 4.
![Page 3: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/3.jpg)
Java program
A Java program, at run-time, is a collection of objects. They
public class TestGreeting {
public static void main(String[] args) {
Greeting gr = new Greeting();
gr.greet();
}
}
public class Greeting {
public void greet() {
System.out.print("Hi there!");
}
}
Đại học Công nghệ - ĐHQG HN Objects and Classes 3
� A Java program, at run-time, is a collection of objects. They
do things (their methods) and ask other objects to do things
(calling methods of others).
� A Java program, when we write it, is a collection of classes
� A Java library contains predefined classes that we can use in
our programs
3
![Page 4: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/4.jpg)
Classes vs. objects
� A class is a blueprint/template that is used to construct objects.
� Each object is instantiated from a class. That object is called an instance of the class.
Đại học Công nghệ - ĐHQG HN Objects and Classes 4
![Page 5: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/5.jpg)
Designing a class
� When you design a class, think about the objects that will be created from that class
� things the object knows about itself
� things the object does
Đại học Công nghệ - ĐHQG HN Objects and Classes 5
![Page 6: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/6.jpg)
Designing a class
� things the object knows about itself
Đại học Công nghệ - ĐHQG HN Objects and Classes 6
���� instance variables
the object's instance variables represent its state
� things the object can do
���� methods
the object's methods represent its behavior
![Page 7: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/7.jpg)
Writing a class
1. Write the class
DOG
size
class Dog {
int size;
String breed;
instance
variables
Đại học Công nghệ - ĐHQG HN Objects and Classes 7
size
breed
name
bark()
String breed;
String name;
void bark() {
System.out.println("Ruff! Ruff!");
}
}
a method
![Page 8: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/8.jpg)
Writing a class
2. Write a tester (TestDrive) class
with code to test the Dog class
public class DogTestDrive {
Đại học Công nghệ - ĐHQG HN Objects and Classes 8
public class DogTestDrive {
public static void main(String [] args) {
Dog d = new Dog();
d.name = "Bruno";
d.bark();
}
}
make a Dog object
set the name of the Dog
call its bark() method
dot notation (.)
gives access to
an object's
instance
variables and
methods
Information hiding is not here yet.
![Page 9: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/9.jpg)
Writing a class
Instance variables/methods belong to an object.
Thus, when accessing them, you MUST specify which object they belong to.
public class DogTestDrive {
Đại học Công nghệ - ĐHQG HN Objects and Classes 9
public class DogTestDrive {
public static void main(String [] args) {
Dog d = new Dog();
d.name = "Bruno";
d.bark();
}
}
access 'name' of the Dog
call its bark() method
dot notation (.)
and
the object
reference
![Page 10: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/10.jpg)
Object references
3 steps of object declaration, creation and assignment:
1. Declare a reference variable
Dog myDog = new Dog();
Đại học Công nghệ - ĐHQG HN Objects and Classes 10
Dog myDog = new Dog();
2. Create an object
Dog myDog = new Dog();
3. Link the object and the reference
Dog myDog = new Dog();
![Page 11: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/11.jpg)
Object references
Dog myDog = new Dog();
Đại học Công nghệ - ĐHQG HN Objects and Classes 11
Remember: References are not objects!
![Page 12: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/12.jpg)
Messaging between objects
� Sending a message to an object is actually calling a method of the object.
d.bark()
� Syntax:
Đại học Công nghệ - ĐHQG HN Objects and Classes 12
<object reference>.<method_name>(<arguments>)
recipient message content extra information
![Page 13: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/13.jpg)
Methods – How objects behave
� Objects have
� state (instance variables)
Đại học Công nghệ - ĐHQG HN Objects and Classes 13
� state (instance variables)
� behavior (methods)
� A method can use instance variables' value and change the object's state.
� A method can use instance variables so that objects of the same type can behave differently
![Page 14: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/14.jpg)
State affects behavior, behavior affects state
1. Write the classDOG
size
breed
name
bark()
getBigger()
class Dog {
int size;
String breed;
String name;
void bark() {
if (size > 14)
State affects behavior.
Dogs of different sizes
behave differently.
Đại học Công nghệ - ĐHQG HN Objects and Classes 14
getBigger()if (size > 14)
System.out.println("Ruff! Ruff!");
else
System.out.println("Yip! Yip!");
}
void getBigger() {
size += 5;
}
}
method changes
state
![Page 15: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/15.jpg)
State affects behavior, behavior affects state
1. Write the classclass DogTestDrive {
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
Đại học Công nghệ - ĐHQG HN Objects and Classes 15
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
one.bark();
}
}
![Page 16: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/16.jpg)
State affects behavior, behavior affects state
1. Write the classclass DogTestDrive {
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
name:null
size:0
breed:null
Dog object 1
one
Đại học Công nghệ - ĐHQG HN Objects and Classes 16
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
one.bark();
}
}
%> java DogTestDrive
![Page 17: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/17.jpg)
State affects behavior, behavior affects state
1. Write the classclass DogTestDrive {
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
name:null
size: 7
breed:null
Dog object 1
one
Đại học Công nghệ - ĐHQG HN Objects and Classes 17
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
one.bark();
}
}
%> java DogTestDrive
![Page 18: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/18.jpg)
State affects behavior, behavior affects state
1. Write the classclass DogTestDrive {
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
name:null
size: 7
breed:null
Dog object 1
one
name:null
size:13
Dog object 2
two
Đại học Công nghệ - ĐHQG HN Objects and Classes 18
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
one.bark();
}
}
%> java DogTestDrive
size:13
breed:null
![Page 19: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/19.jpg)
State affects behavior, behavior affects state
1. Write the classclass DogTestDrive {
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
name:null
size: 7
breed:null
Dog object 1
one
name:null
size:13
Dog object 2
two
Đại học Công nghệ - ĐHQG HN Objects and Classes 19
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
one.bark();
}
}
%> java DogTestDrive
Yip! Yip!
size:13
breed:null
![Page 20: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/20.jpg)
State affects behavior, behavior affects state
1. Write the classclass DogTestDrive {
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
name:null
size: 7
breed:null
Dog object 1
one
name:null
size:18
Dog object 2
two
Đại học Công nghệ - ĐHQG HN Objects and Classes 20
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
one.bark();
}
}
%> java DogTestDrive
Yip! Yip!
size:18
breed:null
![Page 21: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/21.jpg)
State affects behavior, behavior affects state
1. Write the classclass DogTestDrive {
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
name:null
size: 7
breed:null
Dog object 1
one
name:null
size:18
Dog object 2
two
Đại học Công nghệ - ĐHQG HN Objects and Classes 21
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
one.bark();
}
}
%> java DogTestDrive
Yip! Yip!
Ruff! Ruff!
size:18
breed:null
![Page 22: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/22.jpg)
State affects behavior, behavior affects state
1. Write the classclass DogTestDrive {
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
name:null
size: 7
breed:null
Dog object 1
one
name:null
size:18
Dog object 2
two
Đại học Công nghệ - ĐHQG HN Objects and Classes 22
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
one.bark();
}
}
%> java DogTestDrive
Yip! Yip!
Ruff! Ruff!
Yip! Yip!
%>
size:18
breed:null
![Page 23: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/23.jpg)
Compare
size, name vs. bruno, value
syntax?
scope?
Đại học Công nghệ - ĐHQG HN Objects and Classes 23
class Dog {
int size;
String name;
...
void getBigger() {
size += 5;
}
}
public class DogTestDrive {
public static void main(String [] arg
Dog bruno = new Dog();
bruno.name = "Bruno";
...
int value = bruno.size;
}
}
![Page 24: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/24.jpg)
Instance variables
� belong to an object
� declared inside a class but
NOT within a method
� have default values (0, 0.0,
Instance variables vs. local variables
Local variables
� belong to a method
� declared within a method
� MUST be initialized before
use� have default values (0, 0.0,
false, null…)
Đại học Công nghệ - ĐHQG HN Objects and Classes 24
class Dog {
int size;
String name;
...
void getBigger() {
size += 5;
}
}
use
public class DogTestDrive {
public static void main(String []
Dog bruno = new Dog();
bruno.name = "Bruno";
...
int size = bruno.size;
}
}
![Page 25: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/25.jpg)
Encapsulation
� Bad � Better
class Dog {
int size;
String breed;
class Person {
String name;
Date birthday;
Đại học Công nghệ - ĐHQG HN Objects and Classes 25
String breed;
String name;
...
}
Date birthday;
String address;
// about his/her dog
String dogName;
String dogBreed;
int dogSize;
}
class Person {
String name;
Date birthday;
String address;
Dog petDog;
}
![Page 26: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/26.jpg)
Encapsulation / information hiding
� What is wrong with this code?
� It allows for
a supernatural dog
� Object's data is exposed.
class Dog {
int size;
String breed;
String name;
...
}Dog d = new Dog();
d.size = -1;
Đại học Công nghệ - ĐHQG HN Objects and Classes 26
� Exposed instance variables can lead to invalid states of object
� What to do about it?
� write set methods (setters) for instance variables
� hide the instance variables to force other code to use the
set methods instead of accessing them directly.
![Page 27: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/27.jpg)
Information hiding. Rule of thumb
� Mark instance variables private.
� Make getters and setters and mark them public.
class Dog {
private int size;
Đại học Công nghệ - ĐHQG HN Objects and Classes 27
� Don't forget to check data validity in setters.
private int size;
public void setSize(int s) {
if (s > 0) size = s;
}
public int getSize() {
return size;
}
...
![Page 28: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/28.jpg)
Class access control
Access modifiers:
� public : Accessible anywhere by anyone
� private : Only accessible within the current
class
Đại học Công nghệ - ĐHQG HN Objects and Classes 28
� protected : Accessible only to the class itself
and to its subclasses or other classes in the same “package”
� default (no keyword): accessible within the current package
![Page 29: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/29.jpg)
Implementation vs. Interface
� DogTestDrive: a “client” of Dog
� Implementation
� Data structures and code that
implement the object features
(instant variables and methods)
Đại học Công nghệ - ĐHQG HN Objects and Classes 29
(instant variables and methods)
� Usually more involved and may have
complex inner workings
� Clients don’t need to know
� Interface
� The controls exposed to the “client” by the implementation
� The knobs on the black box
![Page 30: Basic Java 03 Objects n Classes](https://reader034.fdocuments.net/reader034/viewer/2022050909/55cf9264550346f57b96028a/html5/thumbnails/30.jpg)
Encapsulation / information hiding
“Don’t expose internal data structures!”
� Objects hold data and code
� Neither is exposed to the end user or "client" modules.
� Interface vs. implementation
A cat's look vs. its internal organs
Đại học Công nghệ - ĐHQG HN Objects and Classes 30
� A cat's look vs. its internal organs
� A TV's screen & buttons vs. the stuff inside the box
� Complexity is hidden inside the object
� Make life easier for clients
� More modular approach
� Implementation changes in one component doesn’t affect others
� Less error-prone