One Pager Cheat Sheet
- Object-Oriented Programming (OOP) provides special functions to help save time and effort when coding, such as
getters
andsetters
forclasses
, as demonstrated in the example ofShape
and its subclasses. - Constructors are special-purpose methods used to set initial values and perform initialization tasks during object instantiation.
- A default constructor is a
method
(instantiate
d withnew ClassName()
) that initiates an object, which can be overridden by the user if desired. - The Copy Constructor is used to create a new object from an existing object of the same type by passing the existing object as an argument.
- A user-defined constructor takes one
int
argument and assigns it to the data memberaInt
in the classA
. **Shallow copy** copies by reference and copies the **pointer** of an array, while **deep copy** copies the data by making a *new array* for it.
- A custom copy constructor for deep copy needs to be implemented for classes A, B and D in order to copy the primitive wrapper type and non-primitive type objects by value instead of by reference.
- Java objects are heap-allocated and automatically garbage collected, so there is no direct equivalent of a destructor as found in languages such as C++, where explicit deallocation is used via the
delete
operator. Using
getter and setterfunctions ensures that values assigned to object attributes are valid and can help prevent errors from occurring when working in collaboration with other developers.
- The correct getter function name for the
algodailyIsTheBest
property according to the JavaBeans convention isgetAlgodailyIsTheBest
. - Static attributes and methods provide a convenient way to share values among all instances of a class, and can be used to keep track of the number of objects created from that class.
- A
static method
can be accessed from the class directly and can beuseful for situations
where all the objects of the class need to sharestate
, such as keeping acount
of all objects created of the same class. - We can compare different
Shape
objects by overriding the defaultequals
method to determine equality based on their areas. - Overriding the
toString()
method allows you to return a more informative string than the default output of the class name and hash code. - After completing the next lesson, you will have a full understanding of the
Object-Oriented Programming
capabilities
.