Java

Thinking in Java Notes (Bruce Eckel)

Is-a vs. is-like-a relationships

  • Points out that pure substitution(the substitution principle) being how you can exactly substitute an object of the derived class for object of the base class. Suggests this is an ideal way to treat inheritance. Regardless it is a good point to make and this a is true case of a "Is-a" relationship.

  • In most cases I've seen what actually happens is that you not only override the base-class methods but you also add new interface elements and end up creating a a new type. The substitution isn't perfect therefore your new methods are not accessible from the base type. This can be described as "is-like-a" relationship.

    • Most devs get taught about "is-a' and "has-"a relationship and it provides more context by framing the "is-a" relationship further as inheritance can be a tricky concept to understanding when first embarking on your Object Oriented jounrney- I know it was for me.

Singly Rooted Hierarchy

Good point to make about all classes ultimately inherited from a single base class.

  • In Java base class is simply Object. There a many benefits of having a singly rooted hierarchy

  1. fundamentally the same class

  2. can be guaranteed to have certain functionality

  3. easier to implement garbage collection

  4. run time type information is guaranteed to be in all objects

    1. important with system-level operations eg. exceptions

Manipulate Objects with References

  • Treat everything as an object

  • However the identifier you manipulate is actually a "reference" to an object"

  • When you create a reference, you want to connect to a new object. eg. String s - new String("dave")

Special case: primitive types

  • boolean, char, byte, short, int, long, float, double, void

  • automatic variable that is created that is not a reference

  • variable holds the value and place on the stack (objects use the heap)

  • all numeric types are signed - no unsigned types

  • size of boolean not explicitly specified - can take literal values true and false

  • "wrapper" classes for the primitive data types allow you to make a nonprimitive object on the heap to represent the primitive type

  • for primitive types they are guaranteed to get a default value if you do no initialise them.