Objects and Classes
Object-Oriented Programming
Outline
Readings:
Introduction to Java
2
Abstract data types
A data type is a set of values and �a set of operations on those values
Primitive types
We want to write programs that process other types of data.
An abstract data type is a data type �whose representation is hidden from the client.
Objects and Classes
3
Object-oriented programming (OOP)
Class == Abstract Data Type
Object == Variables
OOP: create classes and manipulate the objects in your program
(i.e. create your own data types and use them in your program)
Clients can use ADTs �without knowing implementation details.
Our tasks:
Objects and Classes
4
Using a class
How to construct new objects
How to apply operations to a given object
Objects and Classes
5
Color ADT
6
Using color: monochrome luminance
7
Computing with color: grayscale
8
Picture Abstract Data Type
9
Picture client example: Grayscale filter
10
Picture client example: Grayscale filter
11
Objects in a program
12
Object equality
Đại học Công nghệ - ĐHQG HN
More on Java
13
Integer b = new Integer(10);
Integer c = new Integer(10);
Integer a = b;
a==b is true�b==c is false
Integer b = new Integer(10);
Integer c = new Integer(10);
if (b.equals(c)) { // true };
Object equality
Method equals()
Đại học Công nghệ - ĐHQG HN
More on Java
14
Integer m1 = new Integer(10);
Integer m2 = new Integer(10);
System.out.print(m1.equals(m2));
class MyInteger {
private int value;
public boolean equals (Object other) {
….
return (value == other.value);
}
...
}
Object's life on the heap
Đại học Công nghệ - ĐHQG HN
More on Java
15
Object’s life on the heap
Đại học Công nghệ - ĐHQG HN
More on Java
16
class Dog {
String name;
...
public Dog(String n) {
name = n;
}
public Dog(String n, int s) {
name = n;
size = s;
}
}
Dog dog1 = new Dog(“Bruno”);
Dog dog2 = new Dog(“Ben”, 2);
public Dog() {}
Object’s life on the heap
17
Object’s life on the heap
18
Object’s life on the heap
19
Object’s life on the heap
20
Object’s life on the heap
21
Garbage collection
Đại học Công nghệ - ĐHQG HN
More on Java
22
More on Java
22
Đại học Công nghệ. ĐHQG Hà Nội
Classes and Objects
OOP Concepts
23
Classes and Objects
OOP Concepts
24
Objects
OOP Concepts
25
Design a class
26
Picture |
-image: BufferedImage -jframe: JFrame -width, height: int … |
+width(): int +height(): int +get(int, int): Color +set(int, int, Color): void +show(): void +save(String): void … |
instance variables
methods
API, UML notation, Implementation
27
Picture |
-image: BufferedImage -jframe: JFrame -width, height: int … |
+width(): int +height(): int +get(int, int): Color +set(int, int, Color): void +show(): void +save(String): void … |
public final class Picture ... {
private BufferedImage image; // the rasterized image
private JFrame jframe; // on-screen view
private final int width, height;
...
public int height() {
return height;
}
public int width() {
return width;
}
public void show() {
if (jframe == null && !isDisposed) {
jframe = createGUI();
...
...
}
...
}
https://introcs.cs.princeton.edu/java/stdlib/Picture.java
API
UML
Implementation
API: Application Programming Interface.�A list of public instance variables and methods
UML notation: Unified Modelling Language�private members are shown with - �public members are with +
Creating a class
28
Dog |
-size: int -name: String |
+Dog(_name, _size) +bark: void +toString(): String |
public class Dog {
private final String name;
private int size;
public Dog(String _name, int _size) {
name = _name;
size = _size;
}
public void bark() {
if (size > 60) {
System.out.println("Wooof! Wooof!");
} else if (size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
public String toString() {
return "(" + name + ", " + size + ")";
}
}
The Dog class has an instance variable size that the bark() method uses to decide what kind of bark sound to make.
Access modifiers
29
Dog |
-size: int -name: String |
+Dog(_name, _size) +bark: void +toString(): String |
public class Dog {
private final String name;
private int size;
public Dog(String _name, int _size) {
name = _name;
size = _size;
}
public void bark() {
if (size > 60) {
System.out.println("Woof! Woof!");
} else if (size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
}
Access modifiers:
public: accessible by any clients;
private: accessible only from within the class
final: value of the variable will not change once it is initiallized
Instance variables: name and size
Constructor
30
Dog |
-size: int -name: String |
+Dog(_name, _size) +bark: void +toString(): String |
public class Dog {
private final String name;
private int size;
public Dog(String _name, int _size) {
name = _name;
size = _size;
}
public void bark() {
if (size > 60) {
System.out.println("Woof! Woof!");
} else if (size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
}
Constructors: special methods that create an object and provide a reference to that object.
When a client program uses the keyword new, Java will:
Constructors
31
public class Dog {
…
// default constructor
public Dog() { }
// parameterized constructor
public Dog(String _name, int _size) {
name = _name;
size = _size;
}
// parameterized constructor / copy constructor
public Dog(Dog other) {
name = other.name;
size = other.size;
}
...
Compiler will provide an empty default constructor when the class has no constructors
Unlike some other languages, there is no copy constructor provided by default. �Write one yourself if you need it.
Calling another constructor
32
public class Dog {
…
// default constructor
public Dog() {
this(“Grumpy”, 2);
}
// parameterized constructor
public Dog(String _name, int _size) {
name = _name;
size = _size;
}
// parameterized constructor / copy constructor
public Dog(Dog other) {
this(other.name, other.size);
}
...
use the keyword this to call another constructor
Instance methods
Instance methods can perform operations on instance variables.
33
Dog |
-size: int -name: String |
+Dog(_name, _size) +bark: void +toString(): String |
public class Dog {
private final String name;
private int size;
public Dog(String _name, int _size) {
name = _name;
size = _size;
}
public void bark() {
if (size > 60) {
System.out.println("Wooof! Wooof!");
} else if (size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
public String toString() {
return "(" + name + ", " + size + ")";
}
}
The instance variable size affects the bark() method, which uses size to decide what kind of bark sound to make.
A test client
34
public class Dog {
private final String name;
private int size;
public Dog(String _name, int _size) {
name = _name;
size = _size;
}
public void bark() {
if (size > 60) {
System.out.println("Wooof! Wooof!");
} else if (size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
public String toString() {
return "(" + name + ", " + size + ")";
}
}
public class DogTestDrive {
public static void main(String[] args) {
Dog one = new Dog("One", 70);
Dog two = new Dog("Two", 8);
Dog three = new Dog("Three", 35);
one.bark();
two.bark();
three.bark();
}
}
%> java DogTestDrive
Wooof! Wooof!
Yip! Yip!
Ruff! Ruff!
Why “private” size and name?
Objects and Classes
35
class Dog {
int size;
...
}
Dog d = new Dog();
d.size = -1;
Why “private” size and name?
In order to protect the state of an object:
36
public class Dog {
private int size;
public void setSize(int _size) {
if (_size > 0) size = _size;
}
public int getSize() {
return size;
}
...
hide size
setter
getter
Encapsulation
OOP Concepts
37
Picture |
-image: BufferedImage -jframe: JFrame -width, height: int … |
+width(): int +height(): int +get(int, int): Color +set(int, int, Color): void +show(): void +save(String): void … |
related components are inside the box
Information hiding
OOP Concepts
38
Picture |
-image: BufferedImage -jframe: JFrame -width, height: int … |
+width(): int +height(): int +get(int, int): Color +set(int, int, Color): void +show(): void +save(String): void … |
private variables/methods are NOT accessible from outside the class
components are hidden inside the box
Encapsulation/Information hiding
39
declaring all the instance variables in a class as private �and writing public methods in the class to set and get the values of the variables
public class Dog {
private int size;
public void setSize(int _size) {
if (_size > 0) size = _size;
}
public int getSize() {
return size;
}
...
hide size
setter
getter
Class access control
Access modifiers:
Objects and Classes
40
OOP Concept: Abstraction
41
OOP Concept: Abstraction
42
Four pillars of OOP
43
Abstraction
Encapsulation
Inheritance
Polymorphism
Summary
44