Inheritance and Polymorphism II

Here is some info to round out the inheritance topics we've been discussing and help get you through the practice problems, if you choose to do them.
 
 Access Modifiers
  A child class method or variable can be made more public than the one it is overriding/shadowing, but not less public. 
 
 Binding and this
  Although never declared anywhere, this is just another variable referencing an object.  The reference type of this matches the type of the class the currently-executing code is located inside.  But like any other variable, the actual object pointed to by this may instead be a subtype of that reference type.  So static and dynamic binding based on this CAN give different results.

For example:
   class P { f() {...} } 
   class C extends P { ... }
   C obj = new C();
   obj.f();

While in f(), this is of type P but it points to an object of type C.


The stuff below isn't really required knowlege for tests or assignments, but it can be helpful to understand inheritance better---especially when you're trying to debug code that uses a lot of it.

 Constructors & Initializers
Starting at the constructor that is actually invoked with new, things happen in this order: 
  • If there is explicit constructor chaining (this(...)), call it.
  • If there is no explicit chaining:
    • If there is an explicit super call, call it.
    • If there is no explicit super call, call super() automatically.
  • Either way, run data member initializers next.
  • Finally, run the main body of the constructor code.
Remember that an initializer is something like "= 2" at the end of a variable declaration
 

Peter Flynn