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
What is methodological framework?
What is string manipulation?
What is meant by overloading?
What is the difference between conversation & casting?
How many classes can any class inherit java?
Can we clone singleton object in java?
What’s the difference between applets and standalone program?
How do I print a “?
Write an algorithm program in java for the following question.. 1) S is a set of integers.X is an integer obtained by sum of two digits in S. Write logic for whether or not the X is from the S. The time of algorithm should not exceed o(n logn).
What is http client in java?
Explain how to convert any java object into byte array.
What is threaded programming and when is it used? : Java thread
What do you mean by order of precedence and associativity?
What does this () mean in java?
What is java developer skills?