what is the difference between abstract class and
Interface?where we can use it in realtime projects?
Answers were Sorted based on User's Feedback
Answer / king alone
1 A class may inplement several interfaces. A class may
extend only one abstract class.
2 An interface can have only abstract methods. An abstract
class can have both abstract methods and concrete methods.
At one abstract method in the abstract class.
3 Interfaces cannot have constructors. Abstract class can
have constructors.
4 Only static final variables are allowed to be decelared.
Both static and instance variables are allowed.
5 All decelared methods are implicitly public. Can have any
kind of methods.
6 We should initialize variables in an interface. Not
necessary to initialize variables in an abstract class.
7 In the case of interface you can make any class in the
hierarchy. In the case of abstract class you know the class
hierarchy in advance.
8 An interface implementation may be added to any existing
third party class. A third party class must be rewritten to
extend only from the abstract class.
9 Interfaces can be implemented by classes that are not
related to one another Abstract classes are used only when
there is a “is-a” type of relationship between the classes.
Is This Answer Correct ? | 7 Yes | 0 No |
Answer / varun kumar
1.interface contains methods that must be abstract;
abstract class may contain concrete methods.
2.interface contains variables that must be static and
final; abstract class may contain non-final and final
variables.
3.members in an interface are public by default, abstract
class may contain non-public members.
4.interface is used to "implements"; whereas abstract class
is used to "extends".
5.interface can be used to achieve multiple inheritance;
abstract class can be used as a single inheritance.
6.interface can "extends" another interface, abstract class
can "extends" another class and "implements" multiple
interfaces.
7.interface is absolutely abstract; abstract class can be
invoked if a main() exists.
8.interface is more flexible than abstract class because
one class can only "extends" one super class,
but "implements" multiple interfaces.
9.If given a choice, use interface instead of abstract
class.
Is This Answer Correct ? | 3 Yes | 1 No |
Answer / rajesh
An abstract class can be implemented in same packakge while
an interface can be implemented in many package.
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / sunny
Abstract Class | Interfaces
|
1. Starts with a class keyword | Starts with interface
| keyword
2. abstract modifier has to be | No need to provide abstract
provided | modifier (100 % Abstract)
3. Not necessary to contain | All methods are by default
abstract methods, but if a | public and abstract, and
class has abstract method | all attributes are public
then class has 2 be abstract| static, and final (FIELDS)
4. Says what a class should be | Says how a class must
like, behaves like a model | behaves and what it is
| able to do
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / sandeep kumar duey
we can see difference between abstract and inteface class
when we donig thread programming and use Runnable inteface
and Thread class because in case of when we need use
more than one class so in case we use Runnable intface
because we can use more than than one inteface with single
class but can extends only one class .
We also know that java does not support multipal inheritance
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / nitesh
1)We define an Interface in the Java application with a
set of abstract methods without any implementation. Which
means all the methods of an interface are by default
abstract methods and public methods.
2)Once an Interface is defined in the application then any
class can implement that interface, in such case the class
must be implemented with all the mehtods of the respective
Interface otherwise the class will become as abstract class.
3)A class can implement any no. of interfaces, in such case
the class must be implimented with all the methods of those
interfaces otherwise the class will become as abstract class
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / vinod
We use Abstract class and interface to enforce some rules to the classes which extends/implements. For example we can define a class say "Bird" and we can declare methods say "Fly()", "Walk()". This means whatever the class that is derived from Bird has to override or give definition for Fly() and Walk() and therefore we are making sure that all the derived classes follows or has the common functionalities. In other way the classes derived from superclass should have common properties. In addition to this the derive class can have its own methods like "Swim()"...
In case of Abstract class we can define COMMON functionalities in super class and those can be used in the derived class where as in Interface we cant do that. ( this i would say as advantage of abstract class)
In case of Interface the derived class can implement any number of interface but restricted to extend only one abstract class (this i would say as advantage of Interface)
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / khaleek ahmad
Abstract class is a special kind of class which can not be
instantiated while Interface is an Entity. You can define
some methods in Abstract class but can not in interface
For more detail see at
http://interview-preparation-for-you.blogspot.com/2010/10/difference-between-abstract-class-and.html
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / anshul verma
* Major diffrence :- Interfaces are fully implementation of
abstraction
Abstract classes are Partially implementation of abstraction
that's a very important diffrence....
and
The core diffrence between abstract class and interface is
that interface does not allow implementation of method but
abstract class allows.
Interface is a bunch of declaration of relevant functions.
In real time projects, interface can used if we have to
implement any framework.for eg, if want to implement the
framework of stack,we can do it by defining the prototypes
of push(),pop(),peek() methods in an interface and then
implementing it by any class.
Abstract classes is useful when we are not
required to implement all methods in our class.for eg,we
have Vehicle class, we want to implement classes for two
wheelars in which hand brake is given and in some vehicles
foot brake are given, then Vehicle class abstract class can
be defined as abstract class and implemention of brake()
can be done in applicable classes.
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / neha thakur
abstract class contains all abstract method or all concrete
method or combination of both. we can not directly create
object of abstract class.abstract class methods must
implemented in sub class.
interface contains declared method but does not contain body
of method.like abstract class we can not create instance of
interface.methods defined in interface are public &
implicity called as abstract method & interface must be
implemented in the classes with implements keyword which
contains implementation of methods
Is This Answer Correct ? | 0 Yes | 0 No |
Which is better ascii or unicode?
if we give input as " hi how are you" then the output should be "uoy woh"...it should skip odd words in the input and should reverse even words from the end of string...can anyone help me to write this program in java
Should you use singleton pattern?
What is the memory leak in java?
How does java enable high performance?
What are the features in java?
Why webdriver is an interface?
what is jdk1.5 features?
What is composition in java?
Can we add default constructor to Servlet?
what are the different non-access specifiers in java?
what is difference Between Core Java and advance java
60 Answers HCL, HP, Sambalpur University, TCS, Vensai Technologies, Wipro,