Object
representation of a real world entity (book, car, student, etc)
When designing a new system identify all objects that you need to deal with (student, parent, alumni, donation, etc)
OR
a particular instance of a class where the object can be a combination of variables or data structures and functions, procedure, or methods.
Class
a template for creating the objects. It identifies what data needs to be stored (in fields) for the objects of this type and what methods are available to allow access to this data
Constructor method of the class has the same name as the class.
OR
an extensible program-code-template for creating objects, providing initial values for states, and implementations of behaviors.
Instance
when the program has allocated memory to hold the object. The object exists at runtime (instantiation).
What happens to an object when executing a program?
When executing a program the object takes on a different meaning, an object is created by a constructor of the class, and the resulting object is an instance of the class.
Three steps to making an object
Declaration: a variable declaration with a variable name with an object type.
Instantiation: the ‘new’ keyword is used to create the object.
Initialization: the ‘new’ keyword is followed by a call to a constructor, this call initializes the new object.
Example of an object being instantiated (Car is the class)
Car polo = new Car (“VW Polo 1.4”)
Instantiating objects and RAM
Instantiating objects uses RAM
Every time a new object is instantiated from a base class a space equivalent to the whole object is reserved in RAM
RAM can be wasted as it is reserved but not used
Use the most memory efficient variables
How to save memory with objects
Creating a linked list (dynamic data type) of objects is good because only memory actually being used is reserved.
It is bad to create an array (static data type) of objects because potential memory could be wasted in reserving space that might not be needed.
Components of an object
Data (or states) are variables or data structures like arrays/lists
Actions (or behaviour) which can be methods
Objects versus classes
Object - refers to a particular instance of a class, where the object can be a combination of variables or data structures (called states) and functions, procedures, or methods (behaviors).
Class - an extensible program-code-template for creating objects, providing initial values for states (variables) and implementations of behaviors.
Example of an object being instantiated
Car [object template being used] polo [name for the specific instance] = new [java keyword indicating it is the first time this is being done] Car [name of the constructor method to set default values] (“VW Polo 1.4”) [value(s) being sent into the new object as default values]
Car polo = new Car(“VW Polo 1.4”)
UML Diagrams
Associations are shown with lines or arrows.
Shows the objects, variables, and methods.
Association
There are relationships such as structural links
Inheritance
When one class is a subclass of another, meaning it has inherited its characteristics and can also have its own
Cardinality
The possible number of classes that can be had in the relationship
For example for each user class there can be many recipes
Exactly one (1)
Zero or one (0..1)
Zero or more (*)
One or more (1…*)
Ordered (ordered)
Aggregation
Part of relationship
When class 2 is a part of class 1
But they still have separate lifetimes
Composition
A type of aggregation where the parts are destroyed when the whole is destroyed
Class 2 cannot stand by itself, it dies and lives with class 1
Dependency
An object of one class might use an object of another class in the code of a method
If the object is not stored in any field other than this it is modeled as a dependency relationships
Exists between two classes - changes to the definition of one may cause changes to the other (but not vice versa)
Class 1 depends on class 2
Realization
A relationship between the blueprint class and the object containing the respective implementation level details
Eg: the owner interface might specify methods for acquiring property and disposing of property. Both the person and corporation classes implement these methods but in different ways
Decomposition into Several Related Objects
This is turning a problem into objects.
Break down a larger problem into smaller solutions. If you are making a digital clock, it is made of two two digit number displays. Create a class numberDisplay and a class clockDisplay which both is made of two numberDisplay objects.
Breakfast is made of toast which is made of bread and jam
Why should dependancies be reduced between objects in a given problem?
Dependency is bad because it increases maintenance overheads. This means more change has to be made to the entire system to change one component.
Key Data Types
Integer
Real (double)
String
Boolean
Why are different types of data needed?
Data is stored as a combination of binary values in the computer.
Data types are used to store different kinds of data.
They are needed because they specify to the computer how to interpret the binary values in the storage.
How much storage does different data types take up?
Boolean 1 byte
Integer 4 bytes
Real 8 bytes
String - multiple the number of characters by two then add 38, then round up to the next multiple of 8
Parameters
Parameters allow information or instructions to be passed into functions and procedures
Parameters are the names of information that we want to use in a function or procedure
Arguments
The values passed in the parameters
To instantiate an action you need:
Return type
Function name
Parameters
Int product (int x, int y)
Java as a “pass by value” language
Pass an int to the method change() and as a result the change in the value of that int is not reflected in the main method
Java creates a copy of the variable being passed in the method and then does the manipulations
Hence the change is not reflected in the main method