Goldman Sachs Interview Question
Country: United States
I think what the example you gave fits well for interface.The need of overridng is improving a particular method or making it completely new or get the best out of two super classes etc.
method toString in the Object returns
getClass().getName() + "@" + Integer.toHexString(hashCode()).
To convert Integer, Float, Double to string we have to write methods like integerToString, floatToString and doubleToString. Say if we have n classes we have to have to remember N diff method names.
Than this we can override already existing method and return correct values.
Suppose there is a class Company and has method cometToOffice().
- Deven Bhooshan July 11, 2012There are various types of employees in the company. Some(Say All Managers) are having personal Cars and others(Say Salesmen) uses public transport to go office.
Then for both (Manager and Saleman ) you will write separate method comToOfficeByCar() and ComeToOfficeByPublicTransport(). This is not we want in java. If suppose there are 100 ways by which employees go to office. Then you will have to write separate method for them too.
Using OOP what you can do is this. override the ComToOffice method in each subclass of Company and you will not need to remember the names of 100s of various functions.
And also you can put the general functionality in the super class method and call it using super keyword whenever required.
I hope this example helped you somehow :)