Objects, interfaces, and arrays are "reference" data types.
Any reference can be cast to the Object.
Object type determines which method is used at runtime.
Reference type determines which overloaded method will be used at compile time.
There are two types of reference variable casting: downcasting and upcasting.
Downcasting: If you have a reference variable that refers to a subtype object,
you can assign it to a reference variable of the subtype.
You must make an explicit cast to do downcasting.
Upcasting: You can assign a reference variable to a supertype reference variable explicitly or implicitly.
Upcasting is an inherently safe operation because the assignment restricts the access
capabilities of the new variable.
Object reference conversion is permitted when the conversion is towards the top in the
inheritance hierarchy
public class MainClass {
public static void main(String[] argv) {
MyClass myClass = new MyClass();
MySubclass mySub = new MySubclass();
mySub = myClass;
System.out.println();
}
}
class MyClass {
}
class MySubclass extends MyClass {
}
|