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 / sudhakar
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 ? | 125 Yes | 25 No |
Answer / priyanjan
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 ? | 84 Yes | 8 No |
Answer / chitij mehrotra
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 ? | 54 Yes | 6 No |
Answer / abilash
1)If we r having 2 classes, both classes need a method with
same business logic(definition). Then we can go for an
abstract class. In this abstract class we can define the
method with functionality. And the two classes will extend
this abstract class.
2)In other case if two different classes wants two different
functionalities with same method name , then we go for an
interface with abstract declaration of method.And the two
classes will implement the interface and override the method
with different business logic
Is This Answer Correct ? | 31 Yes | 12 No |
Answer / nanda
diff b/w abstract class and interface
1.Abstract class contain both normal fn as well as Abstract
fn wheras Interface can define only abstract fn.
2.by default declared variable in interface is static and
final.
in realtime projects we use inerfaces in AWT OR SWING,where
we implement the inerfaces and use them
Is This Answer Correct ? | 30 Yes | 12 No |
Answer / prabir kumar
1) Classes can implements any no. of interfaces, but
extends only one abstract class.
2) interfaces can be best the choice when your program need
some global variables.
Is This Answer Correct ? | 24 Yes | 8 No |
Answer / rashmi kalra
1. Abstract classes may have concrete methods while
interfaces have no methods implemented.
2. Interfaces do not come in inheriting chain while
abstract class come in inheritance.
Is This Answer Correct ? | 20 Yes | 6 No |
Answer / sachi
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 ? | 18 Yes | 5 No |
Answer / vishal srivastava
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
5. Therefore, Generally given | Thus, given names as
class name as Nouns | Adjectives
6. Used for group of related | Any class can implement
classes only | interfaces
7. Single inheritance is | Multiple inheritance can
allowed only | be achieved
8. All class behaviours are | Cannot have constructors,
allowed, except creating | should initialise its
objects of abstract classes | Fields
Is This Answer Correct ? | 15 Yes | 2 No |
Answer / kala
1) if the class is abstract. this class con't instatiate any
object. if we that class implemented with interface, we can
instatiate object with respect interface methods.
2) if we extend that particular abstract class, we must be
implemented all those abstract methods in that class.
otherwise it will automatically become abstract class. in
interface, if we implements the interface not compalsory
implemented, we can use those class ref.
Is This Answer Correct ? | 47 Yes | 36 No |
Discuss different types of errors that generally occur while programming.
Can you override private or static method in java?
When does an object becomes eligible for garbage collection in java?
What does file separator do in java?
Is Constructor possible in abstract class in java ?
How do you clear an arraylist in java?
In Java, what types of classes perform inheritance?
how to print output with out using sop statements
what do you understand by synchronization? Or what is synchronization and why is it important? Or describe synchronization in respect to multithreading? Or what is synchronization? : Java thread
What is the default size of load factor in hashing based collection?
Can an arraylist be empty?
How do weakhashmap works?