java - How come when I cast down I cannot access the subclass properties? -
class casting { main() { //instantiate subclass motox cellphone myphone = new motox("motorola", "white", true, 5); motox phone = (motox) myphone; //i tried call motox methods using myphone //however netbeans showed properties within cellphone class } }
i know down cast serves convert, if will, super class more specific subtype, when don't gain access motox class' properties.
myphone
defined cellphone
. if want use motox
methods, should either define motox
:
motox myphone = new motox("motorola", "white", true, 5);
or explicitly cast when use it:
cellphone myphone = new motox("motorola", "white", true, 5); ((motox) myphone).somemotoxmethod();
Comments
Post a Comment