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
Is an integer an object?
What is the difference between I ++ and ++ I in java?
What is treemap in java?
What is object-oriented programming?
What is java developer skills?
What is a 16 bit word?
placement papaers of spring computing technology
Hi all, I am dng a mini project on FileSplitter application which splits the GBs of logfile into Smaller chunks(mbs) depending on the split size." How to handle GBs file? I am getting OutOfMemoryException, when I input such GB sized file. Thx
What is initial size of arraylist in java?
What is the purpose of using break in each case of switch statement?
Say any two properties in beans?
If two threads have same priority which thread will be executed first ?
How many bits is a char?
What does java stand for?
What classes of exceptions may be caught by a catch clause in java programming?