Please enable JavaScript.
Coggle requires JavaScript to display documents.
Undestanding Polymorphism (Object vs Reference (rules (1 - The type of the…
Undestanding Polymorphism
A object property take on many different forms.
Object vs Reference
rules
1 - The type of the object determines which properties exist within the object in memory;
2 - The type of the reference determines which properties Java Program can access;
Reference objects in memory
With Superclass
With Interface
Object in memory
can have other type of reference
each type define what is accessible
Casting Objects
Reference same type of object
Can access all properties defined of object
rules
2 - Superclass to Subclass required cast explicity
3 - Compiler not allow casts to unrelated types.
4 - Can throw a exception if the object being cast is not actually an instance of that class.
1 - Subclass to superclass not required cast explicitily
Pay attention
rule 3
class that not have relation
Virtual Methods
Determined in runtime
When override methods in sublcass
Polymorphic Parameters
Method have parameter with Supertype
Can pass any subtype to method
Its behavior will be differently each subtype
e.g. public void m1(Reptile reptile){}
subtype public class Crocodile extends Reptile{}
can pass: m1(crocodile);
m1(reptile);
Code don't compile
Passed class unrelated with supertype
Polymorphism and Method Overriding