java - Call proper subclass method after previous cast to superclass type -


assume have base class bc subclasses sc1, sc2, ... , collection c of objects (some of type sc1 , of type sc2, ...). method m defined in subclasses, not in bc. i'm hoping this:

class anotherclass {   private bc getobj(container c) { /*return c*/ }    private void anothermethod() {     ...     bc o = getobj(c);     o.m();   } } 

now getobj return reference o object , far know must of type bc (or there way apply generics here?). compile error (m() doesn't exist in bc). i'm assuming there there nice way (without having "manually" figure out real type of o somehow)?

why isn't method defined in bc? polymorphism about.

your other option have objects implement interface contains m , have collection of interface instead of bc. or use reflection (ew), or check type (ew).


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -