Inheritance and Polymorphism
In Chapter 8, we learned about classes, objects, and enums. Initially, we explored the relationship between classes and objects and the need to separate the reference type from the object type. We contrasted instance versus class members and saw that using the static
keyword applies class scope to a member. We discussed the this
reference and demonstrated that inside an instance method, the this
reference refers to the object instance responsible for the method call. We also covered various access modifiers: private
, package-private (no keyword), protected
, and public
. These modifiers enable us to apply one of the cornerstones of OOP, namely encapsulation. While encapsulation is commonly referred to as “private data, public methods,” we demonstrated that this does not go far enough due to Java’s call by value mechanism when passing references into and out of methods. We showed how a technique called “defensive copying”...