OBJECTS AND CLASSES - Part 1
Objective
Quiz
- What is the difference between Object and Class?
- What do mean Instantiation?
- What do mean encapsulation?
- Is encapsulation a exclusive property of O.O. languages?
- In the next case "Diego", "Maria" and "Person", which would you consider a Class and which one an Object? Why?
- What is composition?
- Would Color be a good candidate for a user-defined type/class?Why or why not?
Tasks
Objects and Classes
Functional Decomposition
Decompose the software by functionality, break down each functionallity into smaller sub-functions, at the end the smallest functions are coded and tested by a single programmer.
- Data is passed to one function to another.
- Data structure had to be understood in so many places
- Ripple effects are quite easy
- Difficult to debug
Object Oriented Approach
- The data structure is designed first
- The data is encapsulated -> Just one point of understanding
- Virtually no ripple effects.
- Each object is responsible for ensuring the integrity of its own data
What is an Object?
From a software perspective, a (software) object is a software construct/module that bundles together state (data) and behavior (functions) which, taken together, represent an abstraction of a real-world (physical or conceptual) object.
- State/Data/Attributes: An object’s attribute values, when taken collectively, are said to define the state, or condition, of the object.
- Behavior/Operations/Methods: When we talk about software objects specifically, we define an object’s behaviors, also known as its operations, as both the things that an object does to access its attribute values (data) and the things that an object does to modify/maintain its attribute values (data).
What is a Class?
A class is an abstraction describing the common features of all objects in a group of similar objects. A class definition may be thought of as a template for creating software objects.
Instantiation: The term instantiation is used to refer to the process by which an object is created in memory at run time based upon a class definition.
A class defines the features—attributes, methods—that every object belonging to the class must possess; a class can thus be thought of as serving as an object template.
An object, on the other hand, is a unique instance of a filled-in template for which attribute values have been provided, and upon which methods may be performed.
Student y = new Student();
Student y;
Student x;
Student z;
x = new Student();
y = x;
z = new Student();
y = z;
Encapsulation: Encapsulation is a formal term referring to the mechanism that bundles together the state and behavior of an object into a single logical unit.
Garbage Collector
With Java, on the other hand, the JVM periodically performs garbage collection, a process that automatically reclaims the memory of “lost” objects for us while an application is executing.
- If there are no remaining active references to an Object, this will be collected.
Runtime.getRuntime().gc();
User-Defined Types and Reference Variables
y
is a variable that refers to an instance (object) of class Student, y
is known as a reference variable.
int x = 5; //predefined type
Student y = new Student(); //user-defined type
Object as attributes:
If an attribute is declared as reference variable.
Whenever we create a class, such as Student or Professor, in which one or more of the attributes are themselves references to other objects, we are employing an OO technique known as composition. The number of levels to which objects can be conceptually bundled inside one another is endless, and so composition enables us to model very sophisticated real-world concepts.
Benefits
- We avoid data redundancy and the associated potential loss of data integrity.
- Request services of the other Object.
- Reduce memory overhead.
Three Distinguishing Features of an ObjectOriented Programming Language
- User defined (reference) types
- Inheritance
- Polymorphism