when to use abstract class and when to use interface?
Answer Posted / rod
an excelent answer to this question is given by the
following link http://java.sys-con.com/node/36250
apart from the fact that it treats a motor and engine as
equivalent identical items when they are distinct, one
needing elec the other petrol or steam, but the analogy
still holds and is very well illustrated.
In short we use abstract classes to reduce complexity and
duplication, whilst we use interfaces to allow changes in
design without breaking the class hierarchy.
He uses an abstract motor class as a base class to both
solar powered and battery powered vehicles. But what happens
if you must then
describe a third class of vehicle which is a hybrid of these
2 types, you cannot inherit from them both to pick up the
methods of each class so you are stuck having to break the
class hierarchy and redesign.
However If you had used interfaces, you could implement both
the battery and solar car's ability to get the charge time
and minimum light to operate respectively from multiple
interfaces.
| Is This Answer Correct ? | 12 Yes | 4 No |
Post New Answer View All Answers
How do you use substring in java?
What is yield () in java?
When should a function throw an exception?
What is get () in java?
What is string and its types?
What two classes are used to read data only?
What is n in java?
Tell me about your ability to work under pressure
When can you say a graph to be a tree?
What is class and object in java?
What is meant by polymorphism?
What is the difference between variable & constant?
Can I learn java in 3 months?
When a lot of changes are required in data, which one should be a preference to be used? String or stringbuffer?
What is the function of http?