Intermediate OOP Concepts
IS-A, HAS-A, and USES-A relationships: Inheritance, Composition, Aggregation, and Association
Class-Level Relationships
-
IS-A: Inheritance/Generalization: one object is derived from another (a gorilla is-a primate)
Intuition: Gorilla is a kind of primate
Programming Heuristic: extending a class (different from Realization, in which you implement an interface, e.g.)
Instance-Level Relationships
-
IS-A-PART-OF: Composition: one object is composed of another (an arm is-a-part-of a person)
Intuition: Arm objects cannot be shared by other person objects; lifetime of the arm object depends on the person object
Programming Heuristic: one object is an instance variable, created in the constructor, and destroyed in the destructor
-
HAS-A: Aggregation: one object contains another (a university has-a professor)
Intuition: Professor objects can be shared by other university objects; professor objects are not destroyed when a university object is destroyed
Programming Heuristic: one object is an instance reference variable, created in constructor or later
-
USES-A: Association: one object just uses another another (a customer uses-a gas station bathroom key)
Intuition: Key object is used by multiple customer objects; a customer object does not own the key object
Programming Heuristic: a variable is passed as an argument to or returned from a method
REFERENCE: Basic UML Class Diagram Cheat-Sheet
Here are the UML diagram representations of these various OOP relationships: http://creately.com/blog/diagrams/class-diagram-relationships/
Java and OOP
Java is not truly object-oriented as it has primitive types and does
not support the fact that all operations must be performed by sending
messages to objects (e.g., it has static methods you can call without
an object and operator overloading for string concatenation uses
StringBuilder
).
A Pure/Complete/Fully Object Oriented Language is one that supports or has features which treats everything inside program as objects.
It doesn't support primitive datatypes like int, char, float, bool, etc.
There are seven qualities to be satisfied for a programming language to be purely object oriented, like Smalltalk:
- Encapsulation/Data Hiding
- Inheritance
- Polymorphism
- Abstraction
- All predefined types are objects
- All user defined types are objects
- All operations performed on objects must be only through methods exposed at the objects.
Source: https://www.geeksforgeeks.org/java-not-purely-object-oriented-language/
and https://stackoverflow.com/questions/11408427/how-does-the-string-class-override-the-operator
Miscellaneous UML Notes
UML stands for Unified Modeling Language. It is a visual model to
represent your program. It helps readability and understanding of what
your program does, such that even someone that does not know Java or C++
can look at your UML and get an understanding of what is going on. UML
is a diagrammatic language and has become the standard way to draw
software. For those of you that need a refresher in this the link below
will help.
http://www.csci.csusb.edu/dick/cs201/uml.html
This link below is an advertisement for a class that offers UML teaching
but page 2 onwards offers some good graphics of basic UML as well as how
it appears in Java code.
http://www.parlezuml.com/tutorials/umlforjava/java_class_basic.pdf
Ricky J. Sethi, PhD <rickys@sethi.org>
Last updated: Friday, June 15 2018
(sethi.org/tutorials/notes_oop.shtml)