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
Variables used in a switch statement can be used with which datatypes?
What is java’s garbage collected heap?
What is canonical name in java?
What is stream api in java8?
Can we have two methods in a class with the same name?
How we can skip finally block of exception even if some exception occurs in the exception block in java?
What is data string?
Write a program in java to find the maximum and minimum value node from a circular linked list.
Does every java program need a main?
Is assembly language a low level language?
What is ph and buffers?
What is use of set in java?
What is ternary operator in java?
Can we convert list to set in java?
Difference between Preemptive scheduling vs. Time slicing?