|
Self-knowledge is best learned, not by contemplation, but by action. Strive to do your duty and you will soon discover of what stuff you are made. Johann GoetheObjects and Classes
Merriam-Webster's dictionary:
1. refers to something physical.
In the Student Registration System ( SRS ), examples of physical objects are:
2. refers to conceptual objects
formal definition of a software object:
Data:
Examples:
Data of a student
Data of a course:
In object nomenclature, the data elements used to describe an object are referred to as the object's attributes.
An object's attribute values, when taken collectively, are said to define the state of the object.
Example: examine the state of a student to see if she could graduate.
An object's behaviors, also known as operations are both the things an object does to access its data ( attributes ), and the things that an object does to modify/maintain its data ( attributes ).
A student's behavior might include:
A course's behavior might include:
When we actually get around to programming an object in a language like Java, we refer to the programming language representation of an operation as a method, whereas, the term 'operation' is typically used to refer to a behavior conceptually. But, often, these two terms are used interchangeably.
A class is an abstraction describing the common features of all members in a group of similar objects.
A class defines:
| Attribute Name | (Java) Data Type | |
|---|---|---|
| name | String | |
| studentId | String | |
| birthdate | Date | |
| address | String | |
| major | String | |
| gpa | Float | |
| advisor | ??? | |
| courseLoad | ??? | |
| transcript | ??? |
In terms of operations ( methods ), the Student class might define five methods:
The term feature is used to refer interchangeably to either an attribute or a method of a class.
Instantiation is the process by which an object is created/constructed based upon a class definition.
Difference between class and object:
Encapsulation -- the mechanism that bundles together the state information ( attributes ) and behavior ( methods ) of an object into a single unit.
Attributes: name ssn address <etc> Methods: registerForCourse dropCourse chooseMajor <etc> |
Object-record analogy
| studentid | name | major | gpa |
|---|---|---|---|
| 123456 | Arnold Smith | P.E. | 2.3 |
Flaws of correlation between objects and records:
non-OO language
OO language
Just as int is referred to as a simple, or built-in data type in a language like C or Java, we can refer to a user-defined class Student as an Abstract Data Type ( ADT ), which is a user-defined data type that specifies structure as well as behavior.
|
Problem Statment |
|
ADT |
|
Implemented Program ( depends on language ) |
Student y; //instantiation occurs, object created, default constructor used // memory allocated, y is name of the object
Java :
Student y; //instantiation has not occurred,
//noobject created,
//y is a reference variable that has the potential
//to be a reference ( handel ) to a Student objec
//( you may also imagine that y is a pointer )
y = new Student(); //instantiation occurs
| y → |
|
------------------------------------------------------------------------------------
Student x = new Student(); //Object created Student y = x; //y references the same object as x does
|
y →
x → |
|
------------------------------------------------------------------------------------
Student x = new Student(); //Object created Student y = x; //y references the same object as x does Student z = new Student(); //Another object created
|
|
------------------------------------------------------------------------------------
y = z; //y references the same object as z does
|
|
------------------------------------------------------------------------------------
x = z; //x references the same object as z does
|
|
| Attribute Name | Data Type | |
|---|---|---|
| name | String | |
| studentId | String | |
| birthdate | Date | |
| address | String | |
| major | String | |
| gpa | Float | |
| advisor | Professor | |
| courseLoad | ??? | |
| transcript | ??? |
The Professor class, in turn, might be defined to have attributes as follows:
| Attribute Name | Data Type | |
|---|---|---|
| name | String | |
| studentId | String | |
| birthdate | Date | |
| address | String | |
| department | String | |
| studentAdvisee | Student | |
| teaching Assignments | ??? |
Composite class -- a class in which one or more of the attributes are themselves references to other objects
has-a relation
We may encounter a situation where an object 'A' needs to refer to an object 'B', object 'B' needs to refer back to 'A' and both objects need to be able to respond to requests independently of each other.
In this case, we cannot physically embed one object wholly inside the other. ( Why? )
Resolved by using handles ( pointers ) ( C++ : forward declaration )
We are not storing whole objects as attributes inside other objects, but
rather references to objects. The two objects exist separately in memory