and if u have a method in parent which is overriden in child, than obj.method will call the method of child which is overriden. However for static methods this is different, i.e., method of parent will be called
Pls find the foll example
[ May 26, 2005: Message edited by: Nischal Tanna ]
This is perfectly legal and makes use of late binding in Java, one of the most compelling reasons to use OOP.
If I were to declare Sub2 which extends Base, I could have a data structure which contained both Sub and Sub2 instances, then I could just cast each element of my Enumeration to type Base and call any method that Base declares (we'll call it method1() for example). Regardless of whether Sub, Sub2 or both have overridden method1(), the proper method1() for that class will be called.
If I wanted to use methods specific to Sub or Sub2, I would have to cast B1 down the hierarchie to the proper class, which would require either foreknowledge of the runtime class or the use of the instanceof operator.
As to your second question, Sub.submethod() would have to be a static method Otherwise you would need to call (Sub) B1.subMethod(); but see above for the problems which might occur.