What is the difference between Abstract Class and Interface
Answers were Sorted based on User's Feedback
Answer / ranjith
Abstract class:its an incomplete class,containg default
implementation for its sub classes. it contains both
abstract and concrete methids. access specifier
public,private and protected can be used for methods and
variable declaration.atleast on abstract method should be
present in the class. then class extending the abstract
class should override all the abstract methods or the
extended class should be abstract. only one abstract class
can be extended at a time..it contains keyword abstract.it
supports tight coupling.
Interface: all the methods and variable declarations in
interface are either public or abstract.private and
protected acces specifer can be mentioned. it provides
default implementaion for the class that implement it.none
of the methods conatain any method body. the class that
implements should provide implementation for all the
methods in the interface. it provies loose coupling(inter
dependency between the class and interface is less)
Is This Answer Correct ? | 23 Yes | 2 No |
Answer / tarun jangra
1. An abstract class is a specialization whereas Interface
is a generalization. That means an abstract class is created
when only few class wants to share some methods,when you
have to create some methods to be shared by all the classes
then interface is used.
2. An abstract class can have non-abstract methods (means
methods with implementation) while all the methods in an
Interface are by default abstract.
Is This Answer Correct ? | 11 Yes | 0 No |
Answer / ravi
1.A programmer creates abstract class when there are some
common features shared by all the objects.
A programmer writes an interface when all the features
should be implemented differently for different objects.
2.When an abstract class is written the programmer should
provide sub classes to it.
A programmer writes an interface when he wants to leave
implemntation classes to third party vendors.
Is This Answer Correct ? | 8 Yes | 0 No |
Answer / vikrant
Nothing stops you using interfaces and abstract classes
interchangeably. The difference is in the ideology where to
use it.
An abstract class is to be used when one needs to create a
hierarchy. e.g. Poodle, Pointer and Chihuahua are all breeds
breeds of dogs, they have some common characteristics as
dogs and some special characteristics of its breed. so all
breeds (Poodle, Pointer and Chihuahua classes) will inherit
from the Dog class.
Interface on the other hand is just a contract. The
implementing classes need not be related. Say a 3-D figure.
Sphere, Cone, Cylinder are all 3-D shapes they have nothing
in common apart from being 3-D figures. Each will have their
own equation for calculating area, perimeter, volume. All
need to specify in Shapes interface is that the implementing
class must be able to calculate area, perimeter, volume.
Is This Answer Correct ? | 4 Yes | 0 No |
Answer / arun bhat
Abstract class can have normal methods also but Interface
consists of only abstract methods.....
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / guest
An abstract class can have instance methods that implement a
default behavior. An Interface can only declare constants
and instance methods, but cannot implement default behavior
and all methods are implicitly abstract. An interface has
all public members and no implementation. An abstract class
is a class which may have the usual flavors of class members
(private, protected, etc.), but has some abstract methods.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / e.bhavani
1) all abstract methods of the abstract class should be
implemented in its sub classes
all the (abstract) methods of the interface should be
implemted in its implementation classes
2) abstract class can contain instancevariables also
interace can not contation instance varibles. it contations
only constants.
Is This Answer Correct ? | 1 Yes | 3 No |
What is a boolean field?
What is difference between static class and singleton pattern?
What’s the difference between callable and runnable?
What is the purpose of a volatile variable?
What is the difference between this() and super() in java?
What do you meant by active and passive objects?
What is hash code collision?
Describe the various concepts related to object oriented programming (oop).
Why is stringbuffer called mutable?
Explain the advantages of packages in java?
Can we make constructors static?
What is the intersection and union methods?