when to use abstract class and when to use interface?

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the advantage of preparedstatement over statement?

570


Does java return by reference?

537


What do you understand by a Static Variable?

634


Write a program to find maximum and minimum number in array?

554


What is the use of generics? When was it added to the Java development Kit?

558






Can an interface implement another interface?

577


What is final class?

578


What is an interface in java?

563


Can java program run without jre?

572


What are the different collection views provided by maps?

572


How do you sort data in java?

527


Why is java logo a cup of coffee?

627


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

2138


Can a class have multiple constructors?

538


What is the purpose of garbage collection in java?

656