When we will use an Interface and Abstract class?
Answers were Sorted based on User's Feedback
Answer / rahul khurana
Abstract Class :- An abstract class is a special kind of
class that cannot be instantiated. So the question is why
we need a class that cannot be instantiated? An abstract
class is only to be sub-classed (inherited from). In other
words, it only allows other classes to inherit from it but
cannot be instantiated. The advantage is that it enforces
certain hierarchies for all the subclasses. In simple
words, it is a kind of contract that forces all the
subclasses to carry on the same hierarchies or standards.
Interface :-An interface is not a class. It is an entity
that is defined by the word Interface. An interface has no
implementation; it only has the signature or in other
words, just the definition of the methods without the body.
As one of the similarities to Abstract class, it is a
contract that is used to define hierarchies for all
subclasses or it defines specific set of methods and their
arguments. The main difference between them is that a class
can implement more than one interface but can only inherit
from one abstract class. Since C# doesn�t support multiple
inheritance, interfaces are used to implement multiple
inheritance
Is This Answer Correct ? | 23 Yes | 3 No |
Answer / mohd shakir
I think if you have basics knowledge of any OO language then you should be aware of basic definition of Interface and Abstract class.
Here I am going to explain when to use Interface and when to use Abstract class.
Abstract class:-> If you have a common functionality that is used by its subclasses then put that method in super abstract class(now you need not to define that common method in every subclass again and again) This is what you can't do with Interface(because all the methods are by default abstract).
Interface:-> If you don't have the scenario as I discussed above(in abstract class)then choose Interface.And you can also achieve multiple inheritance using Interface that is not possible in case of Abstract class.
Is This Answer Correct ? | 21 Yes | 1 No |
Answer / debapriya patra
Interface:
Through interface we get the multiple inheritance
in java.And we implement interfaces to override the
abstract methods it contains.
Abstract class:
We also use abstract classes to override the
abstract methods that it contains.Otherwise the child class
will mentioned as abstract.
If we wants to prevent a class from instantiation then we
mentioned it as abstract.
Is This Answer Correct ? | 25 Yes | 18 No |
Answer / ravikiran
interface is used when we want all the methods implement
the methods differnetly in different situations.
where as an abstract classis used to define abstarct as
well as non abstract methods
Is This Answer Correct ? | 15 Yes | 9 No |
Answer / devendra.m
Both Absstract classes and interfaces can be used when there
is a difference in subclass types.
When subclass type is totally different then implement the
interfaces.When subclass type is partially common in the
super type then extends the abstract class.Internally some
comman behaviour is there incase of abstract classes.
incase of interface we can achieve multiple inheritace.but
incase of classes we can not achieve multiple inheritace.
Is This Answer Correct ? | 9 Yes | 3 No |
Answer / manoshp
Abstract class:- When there are common behaviors for a set of classes and that behavior do not have any existence by its own, these behaviors are defined in an abstract super class. All sub classes will inherit that common behavior by extending the super class.
Interface:- When there are multiple implementations for a set of functionalities, all these behaviors are defined in an interface as entry points without any implementation details to these behaviors. The client class can use this interface without considering the implementation details.
Is This Answer Correct ? | 6 Yes | 1 No |
Answer / basireddy
interface is used when we want all the methods implement
the methods differnetly in different situations.
Abstract class
We also use abstract classes to override the
abstract methods that it contains.Otherwise the child class
will mentioned as abstract.
Is This Answer Correct ? | 6 Yes | 3 No |
Interface:-> JDK1.5 and above version provide a superb
functionality through interface. That is the main reason
behind using the interface is basically "Interface provide
the loose coupling and also support multiple inheritance".
This is a 100% abstract class. This is also support and
flexible with Polymorphism.
For Example:-
List<String> list = new ArrayList<String>();
Where as List is an interface while ArrayList is an class.
Abstract:-> Abstract class doesn't support multiple
inheritance and if you are creating an any abstract in any
ordinary classes then you must declare abstract. this is
also a major disadvantage of abstract class is that if you
are extend and class or abstract class then you can't extend
any one. Abstract class can't initialize only extended it.
Abstract class may have at least one or more abstract method.
Is This Answer Correct ? | 3 Yes | 1 No |
Answer / abc
interface is used when we want all the methods implement
the methods differnetly in different situations.
Abstract class
We also use abstract classes to override the
abstract methods that it contains.Otherwise the child class
will mentioned as abstract.
Is This Answer Correct ? | 6 Yes | 5 No |
Discuss 2D arrays.
What is keyword auto for?
What is a stack class in java ?
0 Answers Akamai Technologies,
What do you mean by ordered and sorted in collections in java?
Can we override protected method in java?
Can an interface extend another interface?
What is nested loop? What is dangling else condition in it?
What happens if we don’t define serial version uid?
What is args length in java?
Why array is used in java?
What does this () mean in constructor chaining concept?
difference between jsp and java script?