java - How to use subclass methods in superclass? -


i have superclass called parameter. there many different types of parameters , have different methods of course. problem initialize parameter 1 of subs still doesn't allow me use subclass events!

 package com.lbg.c2;  public class parameter {  private  string  type;  public static final parameter numberparam = new numberparam(); public static final parameter stringparam = new stringparam(); public static final parameter anytypeparam = new anytypeparam(); public static final parameter animationparam = new animationparam(); public static final parameter audioparam = new audioparam(); public static final parameter cmpparam = new cmpparam(); public static final parameter combooptionparam = new combooptionparam(); public static final parameter comboparam = new comboparam(); public static final parameter keybparam = new keybparam(); public static final parameter layerparam = new layerparam(); public static final parameter layoutparam = new layoutparam(); public static final parameter objectparam = new objectparam();  public parameter () {  }  public parameter (string t) {     type = t; }  public string gettype() {     return type; }  public static parameter init (string t) {     if (t.equals("number")) {         return numberparam;     }     else if (t.equals("string")) {         return stringparam;     }     else if (t.equals("any type")) {         return anytypeparam;     }     else if (t.equals("animation")) {         return animationparam;     }     else if (t.equals("audio")) {         return audioparam;     }     else if (t.equals("comparision")) {         return cmpparam;     }     else if (t.equals("combo option")) {         return combooptionparam;     }     else if (t.equals("combo")) {         return comboparam;     }     else if (t.equals("keyboard")) {         return keybparam;     }     else if (t.equals("layer")) {         return layerparam;     }     else if (t.equals("layout")) {         return layoutparam;     }     else if (t.equals("object")) {         return objectparam;     }     else {         return new parameter();     } } 

}

this method found statically in parameter class when can init string passed.

        add.addactionlistener(new actionlistener() {          @override         public void actionperformed(actionevent e) {             string types [] = {"number","string","any type","comparision","combo option",             "combo","object","layer","layout","keyboard","animation","audio"};             jcombobox<string> box = new jcombobox<string>(types);             jlabel text = new jlabel ("select parameter type:\n");             jpanel panel = new jpanel();             panel.setlayout(new borderlayout());             panel.add(text, borderlayout.north);             panel.add(box, borderlayout.south);              joptionpane.showmessagedialog(frame, panel, "new parameter", joptionpane.question_message);              parameter p = parameter.init((string) box.getselecteditem());              if (p instanceof numberparam || p instanceof stringparam || p instanceof anytypeparam) {                 string label = joptionpane.showinputdialog(frame,"parameter label?","new parameter",joptionpane.question_message);                 p.setlabel(label); // says needs casting!!             }         }      }); 

here parameter static method must return a subclass of parameter class still doesn't allow me. subclass why doesn't allow me use methods?

it's saying device , subclass phone , can't call others because still known device

a parent class can never knows child classes inherited it. if have define method in child class can accessed by/called upon child class instance . while reverse in not true, if method defined in parent class child class instances can access it/call it.

eg:-

object o = new string("hi"); //will work because object parent class of string  o.trim(); // can't call/will give compilation error because parent object not know child string's trim() if(o.getclass().equals(string.class) ){ // check actual runtime class of o             string ss = (string) o; //typecasting             system.out.println(ss.trim()); // work on string object         } 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -