adspace


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

Write a program to print count of empty strings in java 8?

1086


What do you mean by an interface in java?

1105


Write a java program to find the route that connects between Red and Green Cells. General Rules for traversal 1. You can traverse from one cell to another vertically, horizontally or diagonally. 2. You cannot traverse through Black cells. 3. There should be only one Red and Green cell and at least one of each should be present. Otherwise the array is invalid. 4. You cannot revisit a cell that you have already traversed. 5. The maze need not be in the same as given in the above example

2665


What is a constructor overloading in java?

1125


explain different ways of using thread? : Java thread

1082


What is an object in java and how is it created?

1140


Write a program to find the whether a number is an Armstrong number or not?

1098


How to create a base64 decoder in java8?

1138


What is the difference between equals() and == in java?

1040


What is java string pool?

1083


How to sort array in descending order in java?

996


What are the differences between heap and stack memory in java?

1140


Differentiate between static and non-static methods in java.

1128


Realized?

2264


What is parsing in java?

1041