Citigroup Interview Question
Applications DevelopersCountry: India
You can call only static methods using class name like "child.add(...)" here add should be static.
rules of calling method:
1) if you create pointer of base class and store object of base class then it call method of base class.
2) if you create pointer of child class and store object of child class then it call method of child class
3) if you create pointer of base class and store object of child class then
i) if method is non virtual it call method of base class
ii) if method is virtual it call method of child class
absolutely no issues in complile time or runtime exception.
in the above case, parent class is not extending child hence no issue, it will treated as yet another method in the child class.
if incase it was ovverrided without @override annotation in child, still its absolutly fine
if thete was a @overrride annotation for add(double x, double y).. this is where compiler will complain....
Go thru few concepts in java overriding objects..
In Java , this is not an issue as add(int , int ) will be inherited in the child class . So this will not be method overriding . For method overriding we need to have the same type and number of parameters . This acts as method overloading ,as child class has all the code of the super class in the child class we will have both add( double, double ) and add(int, int) . So it is very simple when we create a child object Child childObj = new Child() ; and do a childObj.add(int , int) then it will call it only .
The syntax is absolutely correct. There would be no compile time error. During method resolving the inherited method with add (int, int) signature from parent class would be resolved and a call to it would be made.
- Nawazish October 15, 2013