Dynamic Types 1
Assignment to a subtype If public Duck extends Bird { Then, you may code:. } Bird bref; Duck quack = new Duck(); bref = quack; A subtype may be assigned where the supertype is expected 2
Assignment to a subtype If public Duck extends Bird { Then, you may code:. } Bird bref; Duck quack = new Duck(); bref = quack; Static Type: bref is declared as a reference to a Bird object Dynamic Type: bref is a reference to a Duck object! 3
Static Type Evaluated at compile time Almost always specified by the programmer Used by the compiler to issue warning and error messages 4
Implicit Up-cast Cast : Treat a Duck object as if it were an object of type Bird bref = (Bird) quack; Up-cast : Cast of a sub-type to a super-type (widening) Down-cast: Cast of a super-type to a sub-type (narrowing) Implicit Up-cast : Compiler performs the up-cast without direction from the programmer bref = quack; No compiler messages! Note Compiler requires EXPLICIT down-cast 5
Dynamic Type Evaluated at run-time Java keeps track of the ACTUAL type of a reference, even when the reference has been implicitly up-cast to a super-type. Most often, the type specified after new Duck quack = new Duck(); Or, result of method return value Dynamic type is used to select the VMT Enables polymorphism! Dynamic type of quack Dynamic type checked at run time when explicit down-cast occurs 6
Down-casting Requires sophisticated programmer to get it right Programmer tells compiler: Even though you think this is the super-type, I know that it s really the sub-type. If the programmer is wrong, run-time errors occur! Use instanceof to ensure you ve got it right if (bref instanceof Duck) { Duck bref2 = (Duck) bref; See also: Univesity of Texas Java Class 7
Casting and Operator Precedence Cast occurs before normal operators (double) sum / array.length; But. occurs before cast (double) array.length Extra parenthesis required to override. (Alpha)var1.mk(); // cast result of var1.mk() to Alpha ((Alpha)var1).mk(); // cast var1 to Alpha // Invokes Alpha class mk method 8
Static AND Dynamic Types You must pay attention to both static and dynamic types Static types are used by the compiler to determine whether instructions are legal Dynamic types are used for method dispatch 9
Private Methods A private method is not visible from outside the class If a private method is invoked from inside the class it will ALWAYS run the class method, even if the receiver is a sub-type that overrides that method! 10
Example Private Method public class PARENT { private void m1() { System.out.println( parent ); } public static void main(string[] args) { Parent p = new Child(); p.m1(); } } public class Child extends Parent { public void m1() { System.out.println( child ); } } Prints : parent @Override causes compiler error 11
Default Method Visibility If you do not specify private, public, or protected, you get package private visibility package private methods behave like public methods within the package package private methods behave like private methods if invoked from outside the package i.e. invisible 12
Protected Methods Behave like public methods if invoked within the package Visible within a descendant class, even if outside the package Invisible outside package from non-descendant classes! 13
Method Visibility Overridable Invocable Private No Within class Package Private Within package Within package Protected Within package or descendant class Within package or descendant class Public Yes Everywhere 14
Dynamic Dispatching When a method is invoked, uses the VMT associated with the dynamic type to find out what method to invoke Enables polymorphism Dynamic dispatching does NOT occur for private methods! In this case, private methods are only visible within the class Dynamic dispatching does NOT occur for static methods Static methods resolved from <classname>.class 15