Professional Documents
Culture Documents
38 Object Concepts
38 Object Concepts
15-Apr-18
Concept: An object has behaviors
2
Concept: An object has state
3
Example: A “Rabbit” object
4
Concept: Classes describe objects
5
Concept: Classes are like Abstract Data Types
class Employee {
// Fields
private String name; //Can get but not change
private double salary; // Cannot get or set
// Constructor
Employee(String n, double s) {
name = n; salary = s;
}
// Methods
void pay () {
System.out.println("Pay to the order of " +
name + " $" + salary);
}
public String getName() { return name; } // getter
}
7
Approximate Terminology
instance = object
field = instance variable
method = function
sending a message to an object =
calling a function
These are all approximately true
8
Concept: Classes form a hierarchy
9
Example of (part of) a hierarchy
Container
Dialog Frame
FileDialog
A FileDialog is a Dialog is a Window is a Container
10
C++ is different
11
Concept: Objects inherit from superclasses
12
Example of inheritance
15
Notation: How to reference a field or method
16
Concept: this object
17
Concept: A variable can hold subclass objects
Suppose B is a subclass of A
A objects can be assigned to A variables
B objects can be assigned to B variables
B objects can be assigned to A variables, but
A objects can not be assigned to B variables
Every B is also an A but not every A is a B
You can cast: bVariable = (B) aObject;
In this case, Java does a runtime check
18
Example: Assignment of subclasses
myDog = rover; // ok
yourPoodle = fifi; // ok
myDog = fifi; //ok
yourPoodle = rover; // illegal
yourPoodle = (Poodle) rover; //runtime check
19
Concept: Methods can be overridden
22
Concept: Constructors make objects
23
Syntax for constructors
24
Trick: Give field and parameter the same name
A parameter overrides a field with the same name
But you can use this.name to refer to the field
class Person {
String name;
int age;
25
Internal workings: Constructor chaining
26
The case of the vanishing constructor
27
Example: Broken constructor chain
class Person {
String name;
Person (String name) {
this.name = name;
}
} Java tries to execute
class Employee extends Person { an implicit super()
double salary; at this point
Employee ( ) {
super();
salary = 12.50;
}
}
cannot resolve symbol – constructor Person()
28
Fixing a broken constructor chain
29
Trick: one constructor calling another
class Something {
Something (int x, int y, int z) {
// do a lot of work here
}
Something ( ) { this (0, 0, 0); }
}
30
Concept: You can control access
class Person {
public String name;
private String age;
protected double salary;
public void birthday { age++; }
}
Each object is responsible for its own data
Access control lets an object protect its data and
its methods
Access control is the subject of a different lecture
31
Concept: Classes can have fields and methods
32
Example of a class variable
class Person {
String name;
int age;
static int population;
Person (String name) {
this.name = name;
this.age = 0;
population++;
}
}
33
Advice: Restrict access
34
Advice: Use setters and getters
36
The End
37