When we will use an Interface and Abstract class?

Answers were Sorted based on User's Feedback



When we will use an Interface and Abstract class?..

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

When we will use an Interface and Abstract class?..

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

When we will use an Interface and Abstract class?..

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

When we will use an Interface and Abstract class?..

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

When we will use an Interface and Abstract class?..

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

When we will use an Interface and Abstract class?..

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

When we will use an Interface and Abstract class?..

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

When we will use an Interface and Abstract class?..

Answer / sumit sharma

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

When we will use an Interface and Abstract class?..

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

Post New Answer

More Core Java Interview Questions

What are the differences between this and super keyword?

0 Answers  


Give few difference between constructor and method?

0 Answers  


How many types of constructors are used in java?

0 Answers  


what is difference between interface and abstract class..?

4 Answers  


What is the meaning of flag day?

0 Answers  






Does substring create a new object?

0 Answers  


What is array sorting in java?

0 Answers  


Why is it called a string?

0 Answers  


I have a String s = java; What is the output when I say s.replaceAll('j', 'k'); Also what is the value of s after replacing?

8 Answers   KPIT,


Who developed JScript language?

3 Answers  


Is intellij better than eclipse?

0 Answers  


what is default layout of JFrame class?

6 Answers  


Categories