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

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -