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


Please Help Members By Posting Answers For Below Questions

What is synchronization and why is it important in java programming?

473


What is scanner in java?

547


What are the approaches that you will follow for making a program very efficient?

652


What happens if a try-catch-finally statement does not have a catch clause to handle an exception that is thrown within the body of the try statement?

666


What is a parameter in simple terms?

564






How can we break singleton in java?

528


Is array dynamic in java?

485


Why is it important to initialize a variable?

500


Write a java program to check if a number is prime or not?

539


What do you mean by ternary operator in java?

562


Why is logger singleton?

564


What language is an assembler written in?

559


What is qms certification?

526


Is it necessary that each try block must be followed by a catch block?

564


Why to use nested classes in java?

592