Professional Documents
Culture Documents
Programming
Mihai Dasclu
2CB
Thinking in Objects
OO Programming Concepts
Objects
Class Name: Circle
A class template
Data Fields:
radius is _______
Methods:
getArea
Circle Object 2
Circle Object 3
Data Fields:
radius is 10
Data Fields:
radius is 25
Data Fields:
radius is 125
Three objects of
the Circle class
Object Oriented Programming
Circle Object 1
Classes
Classes
class Circle {
/** The radius of this circle */
double radius = 1.0;
/** Construct a circle object */
Circle() {
}
Data field
Constructors
Method
Class name
radius: double
Data fields
Circle()
Constructors and
methods
Circle(newRadius: double)
circle1: Circle
radius = 1.0
circle2: Circle
radius = 25
circle3: Circle
radius = 125
UML notation
for objects
getArea(): double
Constructors
Constructors
new Circle(5.0);
Circle() {
}
Circle(double newRadius) {
radius = newRadius;
}
new Circle();
Default Constructor
A class may be declared without constructors. In
this case, a no-arg constructor with an empty body
is implicitly declared in the class => default
constructor provided automatically only if no
constructors are explicitly declared in the class
10
To
Example:
Circle myCircle;
ClassName objectRefVar;
11
Declaring/Creating Objects
in a Single Step
ClassName objectRefVar = new
ClassName();
Assign object reference
Example:
Create an object
12
Accessing Objects
Referencing
objectRefVar.data
Invoking
objectRefVar.methodName(arguments)
e.g., myCircle.getArea()
e.g., myCircle.radius
13
Example
Trace the construction of
myCircle, yourCircle
yourCircle.radius = 100;
14
Caution
No. All the methods used before this chapter are static
methods, which are defined using the static keyword.
However, getArea() is non-static. It must be invoked from
an object using
objectRefVar.methodName(arguments)
(e.g., myCircle.getArea())
15
The
16
17
Variable initialization
Compilation error:
variables not initialized
18
int i = 1
Object type
Circle c
reference
c: Circle
radius = 1
Primitive type
19
After:
Before:
c1
c1
c2
c2
c1: Circle
C2: Circle
c1: Circle
C2: Circle
radius = 5
radius = 9
radius = 5
radius = 9
20
TIP: If you know that an object is no longer needed, you can explicitly
assign null to a reference variable for the object (or invoke clear()). The
JVM will automatically collect the space if the object is not referenced by
any variable
Garbage Collection
21
+Date(elapseTime: long)
+toString(): String
+getTime(): long
22
23
+Random()
+Random(seed: long)
+nextInt(): int
+nextLong(): long
+nextDouble(): double
+nextFloat(): float
+nextBoolean(): boolean
java.util.Random
24
From random1: 734 660 210 581 128 202 549 564 459 961
From random2: 734 660 210 581 128 202 549 564 459 961
25
26
circle1
radius = 1
numberOfObjects = 2
Circle
radius: double
numberOfObjects: int
getNumberOfObjects(): int
+getArea(): double
UML Notation:
+: public variables or methods
underline: static variables or methods
instantiate
Memory
1
radius
numberOfObjects
radius
circle2
radius = 5
numberOfObjects = 2
27
public
The class, data, or method is visible to any class in
any package
private
The data or methods can be accessed only by the
declaring class
28
Visibility
package p1;
package p2;
public class C2 {
void aMethod() {
C1 o = new C1();
can access o.x;
can access o.y;
cannot access o.z;
package p1;
class C1 {
...
}
public class C3 {
void aMethod() {
C1 o = new C1();
can access o.x;
cannot access o.y;
cannot access o.z;
package p2;
public class C2 {
can access C1
}
public class C3 {
cannot access C1;
can access C2;
}
public class C1 {
public int x;
int y;
private int z;
29
NOTE
An object cannot access its private members, as
shown in (b). It is OK, however, if the object is
declared in its own class, as shown in (a).
30
protect data
To
-numberOfObjects: int
+Circle()
+Circle(radius: double)
+getRadius(): double
+getNumberOfObject(): int
+getArea(): double
31
32
Heap
A circle
object
Stack
33
Array of Objects
Circle[] circleArray = new Circle[10];
An
circleArray
circleArray[1]
circleArray
reference
Circle object 0
Circle object 1
circleArray[9]
Circle object 9
Invoking
34
35
Example
36
Cachable
37
java.lang.String
Enum classes
java.io.File
java.util.Locale
java.net.Inet4Address, java.net.Inet6Address,
Java.net.InetSocketAddress
Examples of Immutable
classes
38
Scope of Variables
39
40
41
Calling Overloaded
Constructor
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
this must be explicitly used to reference the data
public Circle() {
this(1.0);
}
42
Class implementation
is like a black box
hidden from the clients
Class
Class Contract
(Signatures of
public methods and
public constants)
43
Example: The
StackOfIntegers Class
-elements: int[]
-size: int
+StackOfIntegers()
+StackOfIntegers(capacity: int)
+empty(): boolean
+peek(): int
+pop(): int
Removes the integer at the top of the stack and returns it.
+getSize(): int
StackOfIntegers
44
Designing the
StackOfIntegers Class
Data2
Data3
Data2
Data1
Data1
Data3
Data2
Data2
Data1
Data1
Data1
Data3
Data2
Data1
Object Oriented Programming
Data1
45
Implementing
StackOfIntegers Class
elements[capacity 1]
.
.
.
top
.
.
.
size
elements[1]
elements[0]
capacity
bottom
elements[size-1]
46
Designing a Class
You can use a class for students, but you should not combine
students and staff in the same class, because students and
staff have different entities
47
48
49
50
Superclasses and
Subclasses
-color: String
-filled: boolean
-dateCreated: java.util.Date
+GeometricObject()
Creates a GeometricObject.
+GeometricObject(color: String,
filled: boolean)
+getColor(): String
+isFilled(): boolean
+getDateCreated(): java.util.Date
+toString(): String
Rectangle
Circle
-radius: double
-width: double
+Circle()
-height: double
+Circle(radius: double)
+Rectangle()
+getRadius(): double
+getWidth(): double
+getArea(): double
+getPerimeter(): double
+getHeight(): double
+getDiameter(): double
+printCircle(): void
+getArea(): double
+getPerimeter(): double
GeometricObject
51
Are superclasss
Constructor Inherited?
52
Superclasss Constructor Is
Always Invoked
A constructor may invoke an overloaded
constructor or its superclasss constructor. If none
of them is invoked explicitly, the compiler puts
super() as the first statement in the constructor.
For example,
public A() {
}
is equivalent to
public A() {
super();
}
public A(double d) {
// some statements
}
is equivalent to
public A(double d) {
super();
// some statements
}
53
CAUTION
You must use the keyword super to call the superclass
constructor
Invoking a superclass constructors name in a subclass
causes a syntax error. Java requires that the statement that
uses the keyword super appear first in the constructor
54
Constructor Chaining
Constructing an instance of a class invokes all the
superclasses constructors along the inheritance chain. This
is called constructor chaining.
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employees overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
55
class Fruit {
public Fruit(String name) {
System.out.println("Fruit's constructor is invoked");
}
}
56
Declaring a Subclass
57
58
Note
59
class B {
public void p(double i) {
System.out.println(i * 2);
}
}
class B {
public void p(double i) {
System.out.println(i * 2);
}
}
class A extends B {
// This method overrides the method in B
public void p(double i) {
System.out.println(i);
}
}
class A extends B {
// This method overloads the method in B
public void p(int i) {
System.out.println(i);
}
}
60
Equivalent
61
A a= new A();
System.out.println(a.toString());
62
63
Cn-1
.....
C2
C1
Dynamic Binding
64
65
Generic Programming
Polymorphism allows
methods to be used
generically for a wide range
of object arguments =>
generic programming
If a methods parameter
type is a superclass (e.g.,
Object), you may pass an
object to this method of any
of the parameters
subclasses (e.g., Student or
String)
When an object (e.g., a
Student object or a String
object) is used in the
method, the particular
implementation of the
method of the object that is
invoked (e.g., toString) is
determined dynamically
66
Casting Objects
Casting
m(new Student());
67
68
Casting from
Superclass to Subclass
Explicit casting must be used when casting an
object from a superclass to a subclass. This type of
casting may not always succeed.
GraduateStudent x =
(GraduateStudent)stud;
Student x = (Student)person;
Object Oriented Programming
69
70
Apple
Orange
Tip
71
72
Note
73
Visibility increases
private, none (if no modifier is used), protected, public
74
Class
Package
Subclass
World
public
protected
no
modifier
private
75
76
A class should use the private modifier to hide its data from
direct access by clients. You can use get methods and set methods
to provide users with access to the private data, but only to
private data you want the user to see or to modify. A class should
also hide methods not intended for client use
77
public class C2 {
C1 o = new C1();
can access o.x;
can access o.y;
can access o.z;
cannot access o.u;
}
public class C3
extends C1 {
can access x;
can access y;
can access z;
cannot access u;
}
public class C4
extends C1 {
can access x;
can access y;
cannot access z;
cannot access u;
public class C5 {
C1 o = new C1();
can access o.x;
cannot access o.y;
cannot access o.z;
cannot access o.u;
package p2;
78
79