java - Call method in subclass -
my code following
class shape { void drawshape() { system.out.println("shape drawn of shape class"); } void printstr() { system.out.println("checking"); } } class circle extends shape { void drawshape() { system.out.println("shape drawn of circle class"); } public void printnumber() { system.out.println("1"); } } public class testshape { public static void main(string[] args) { shape shape = new circle(); shape.drawshape(); shape.printstr(); } }
here output
shape drawn of circle circle checking
when calling shap.drawshape(), executing method of subclass, when trying call shape.printnumber() compiler giving error , question same object if sub class method executing, (i know overriding) why can't call subclass method?
because java statically-typed , declaration of subclass. able call printnumber,
must use:
circle shape = new circle(); shape.drawshape(); shape.printstr();
drawshape()
works fine due polymorphism.
Comments
Post a Comment