when to use abstract class and when to use interface?

Answers were Sorted based on User's Feedback



when to use abstract class and when to use interface?..

Answer / amit

Abstract provides both methods with and without body.
Adding new methods in abstract class in middle of
development life cycle won't break existing sub classes
unless these methods are declared as mustoverride. If there
are frequent addition of new methods properties and so on.
one should use abstract..

Whereas on the other hand interface can fill gap of
multiple inheritance. One can
implement from more than one interface. Not same with
Abstract. One can inherit
from only one abstract class.

Is This Answer Correct ?    44 Yes 6 No

when to use abstract class and when to use interface?..

Answer / muthusenthil

Abstract(patially implemented) class can be used as a
common base class for an application it has all property of
class except it cannot be instantiated.it provide default
behavior.

interface is mearly a specification,nothing implemented for
any standalone project which can be changed at will its
more design flexible and it can be utilized to model
multipleinheritance.

Is This Answer Correct ?    56 Yes 30 No

when to use abstract class and when to use interface?..

Answer / rod

"Interfaces are often used to describe the peripheral
abilities of a class, not its central identity, e.g. an
Automobile class might implement the Recyclable interface,
which could apply to many otherwise totally unrelated objects."
a cricketball class may implement the class bowlable which
could apply to many otherwise totally unrelated objects such
as BowlingBall or LawnBowlsBall or TenpinBowlingBall.

"An abstract class defines the core identity of its
descendants. If you defined a Dog abstract class then
Dalmation descendants are Dogs, they are not merely dogable.
Implemented interfaces enumerate the general things a class
can do, not the things a class is."
a Cricketball might extend/inherit the Ball abstract class
as above and in so doing recognise that a ball is an entity
with the common properties of diameter and volume not just
bowlable. here both abstract class and interface can be used
together.

One insightful difference and therefore an indicator of
usage is this. If an abstract class has all its methods
required to be overridden so that there is no common
behavior implemented in it (that its subclasses can use),
then you have effectively... Interface.
"If all the methods of an abstract class are uncompleted
then it is the same as an interface."..said this developer
or put another way , the more you implement methods in the
class for use by its descendants, the more you move away
from an interface and more towards an abstract class. So
interfaces are concerned with behaviors that are common in
concept but different in implementation and refer therefore
to exchanges between components at a system level. Typically
interfaces are used at the periphery of components so that
they can interface to other components that may be
implemented using possibly different languages for example.
Abstract classes however are concerned with behaviors that
are common in concept and in implementation.

Is This Answer Correct ?    30 Yes 4 No

when to use abstract class and when to use interface?..

Answer / mohit jethva

In Interfate you are restricting a user to impement each
and every method of interface so if you have a requirement
which is constantly changing so don't prefere interface.

While in Abstract Class user don't need to implement each
and every method which is not required or marked with
mustoverride.


And by using interface you can achive multiple inheritance
in C# but with Abstact class it's can't be achive

Is This Answer Correct ?    31 Yes 21 No

when to use abstract class and when to use interface?..

Answer / devarathnam c,kotagudibanda(po

Hi...
When u want to inherit the properties of base class only
(not other resources) go for Abstract class.
When u want to inherit the properties of base class and
other resources go for Interfaces .

Is This Answer Correct ?    42 Yes 34 No

when to use abstract class and when to use interface?..

Answer / 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

when to use abstract class and when to use interface?..

Answer / ravikiran

abstract class is used if you are willing to restrict the
creation of an instance.
interface is used if you are willing to write different
functionalities in different implementations.

Is This Answer Correct ?    34 Yes 29 No

when to use abstract class and when to use interface?..

Answer / praveen reddy.e

generally abstract class is written when there are some common features shared by all the objects as they are.

for eg take a class Wholesaler which represents a whole sale shop with text books and stationary like pens,note books etc

class wholesaler
{
void text_books()
{
//text books of X class
}
void stationary();//this can be pens,papers or note books
}

Let us take a Retailer1,a class which represents a retail shop.Retailer1 wants text books of X class and some pens.similarly,Retailer2 also wants text books of X class and some papers.In this case we can understand that the void text_books() is the common feature shared by both the Retailers.But the stationary asked by the Retailers is different.This means ,The stationary has different implementations for different retailers but there is a common feature,i.e,the text books.So in this case,the programer design the Wholesaler class as abstract class.Retailer1 ane Retailer2 as sub classes.
On the other hand, the programer uses an interface if all the features need to be implemented differently for different objects.Suppose,Retailer1 asks for VII class text books and Retailer2 asks for X class books,then even the text_books() of Wholesaler class needs different implementations depending upon the retailer.It means ,the void text_books() and also void stationary() method should be implemented differently depending upon the retailer.So in this case the programer desings the Wholesaler as an interface and Retailer1 and Retailer2 become implementaion classes

Is This Answer Correct ?    4 Yes 0 No

when to use abstract class and when to use interface?..

Answer / brijendra kumar soni(xavient)

1-By using Interface we can achieve the Multiple
inheritence while by using abstract class we can not
achieve.

2-Abstract Class extended by a class only while Interface
implmented by a class and extended to one or more
interfaces.

Is This Answer Correct ?    1 Yes 0 No

when to use abstract class and when to use interface?..

Answer / rod

The above answer is wrong because it uses the word
"interface" to define itself. You simply cannot say "which
are available in the interface go for interface" because you
cannot use a word in the description of itself. It is like
saying a what is a ball...well a ball is a ball. Which is
correct but does not answer the question.
Whoever wrote the above answer does not speak english as a
first language and therefore they are confusing the issue.

Is This Answer Correct ?    1 Yes 1 No

Post New Answer

More Core Java Interview Questions

what is platform independence in java?

5 Answers   CSC, HSBC,


Explain, why the constructor is required in implemented class?

4 Answers  


What is byte code and why is it important to java’s use for internet programming?

0 Answers  


what is thread? : Java thread

0 Answers  


what is difference between colection and collections?

25 Answers   Amdocs, IBM, Infosys, Ipog Software, Polaris, Tech Mahindra,






What is the difference between array list and vector in java?

0 Answers  


What does indexof mean?

0 Answers  


How to sort the elements in HashMap

3 Answers   Ness Technologies,


How are destructors defined in java?

0 Answers  


I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate data in that objet with out using Set?

8 Answers   Aricent,


What is a instance variable?

0 Answers  


how to print the below in java?thanks in advance.... * * * * * * * *

3 Answers  


Categories