i have 2 classes, called superclass and subclass, i tried to cast the subclass object to superclass, but its seems does not work when i want to use the subclass object from the superclass. Please help to explain. Thanks. These are the code:-
public class superclass
{
public void displaySuper()
}
System.out.println("Display Superclass");
}
}
public class subclass extends superclass
{
public void displaySub()
{
System.out.println("Display Subclass");
}
}
public class Testing
{
public static void main(String args[])
{
subclass sub = new subclass();
superclass sup = (superclass) sub;
when i tried to use the displaySub() from the subclass get error
sup.displaySub(); //the displaySub method cant found
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…