What is the difference between Abstract class and Interface


Answered by Scott on 2005-05-12 10:03:06: An abstract class can contain non-abstract methods which do not have to be overridden in the subclass. 
 
There is no difference between a fully abstract class (all methods declared as abstract and all fields are public static final) and an interface.

Showing Answers 1 - 54 of 54 Answers

Scott

  • May 12th, 2005
 

An abstract class can contain non-abstract methods which do not have to be overridden in the subclass. 
 
There is no difference between a fully abstract class (all methods declared as abstract and all fields are public static final) and an interface.

  Was this answer useful?  Yes

Mayank

  • Jul 26th, 2005
 

A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class. Additionally, a class extending an abstract class can implement an infinite number of it's own methods.

  Was this answer useful?  Yes

Buddhadev Dasgupta

  • Jan 19th, 2006
 

another key difference between abstract class and interface is that , in abstract class we can put sharable code, but that is not possible in case of interface.

Another key difference between abstract class and interface is that,

We can use interface as marker, ( we can use abstract class also as abstract but then we can't extends any oter class, so it is better always use interface as marker)

  Was this answer useful?  Yes

PATIL SHIVAJI KISAN

  • Jan 24th, 2006
 

what is the difference between abstraction and encapsulation?

  Was this answer useful?  Yes

Nandesh

  • Feb 20th, 2006
 

what do you mean by marker?

Ram

  • Apr 18th, 2006
 

        An interface having no methods is called as a Marker Interface.

  Was this answer useful?  Yes

Prasanth

  • May 12th, 2006
 

Abstract class does not support Multiple Inheritance . Interface supports Multiple Inheriatnce..

  Was this answer useful?  Yes

yugandhar

  • Jul 17th, 2006
 

marker is nothing but like an indication, which means it says some intimation to the JAM or Container. For example, we want to store an object in the file then we have to intimate to the compiler that this object should be stored. This intimation can be done by implementing the interface java.io.Serializable.

  Was this answer useful?  Yes

varun mittal

  • Aug 24th, 2006
 

The differnce is that in interface all are public but in abstract class u can have private and protected members

  Was this answer useful?  Yes

Rupesh

  • Sep 21st, 2006
 

All I know is that interface is a contract and abstract class is for generalization but I could not understand when to use abstract class and when to use interface?

Can anyone explain with example?

 

 

  Was this answer useful?  Yes

sonal watane

  • Nov 6th, 2006
 

 encapsulation the wrapping up of data into a single unit.

     whereas abstraction refers to the act of representing essential features

without including the background details.

  Was this answer useful?  Yes

Palanisami

  • Apr 5th, 2007
 

1. Abstract Class may contains constructor but interface does not contain constructor.

2.By default, all variables in interfaces are static and final,then we can't declare method as final.

3.In Interface all methods shuld be public but mot in abstract class.

4.interface supportes multiple inheritance but abstract class does not support.

5.we should initialise variables in interface but not necessory in abstract class.

sripal reddy

  • Jun 30th, 2007
 

An abstract class is class with zero or more abstract methods. An interface is a specification of method prototype.

  Was this answer useful?  Yes

Akanksha Rakesh

  • Jul 24th, 2007
 

In design, you want the base class to present only an interface for its derived classes. This means, you don’t want anyone to actually instantiate an object of the base class. You only want to upcast to it, so that its interface can be used. This is accomplished by making that class abstract using the abstract keyword. If anyone tries to make an object of an abstract class, the compiler prevents it. The interface keyword takes this concept of an abstract class a step further by preventing any method or function implementation at all. You can only declare a method or function but not provide the implementation. The class, which is implementing the interface, should provide the actual implementation.

  Was this answer useful?  Yes

Raja Pachamuthu

  • Aug 22nd, 2007
 

Absraction: The process of extracting essential information and hiding irrelevent details

Encapsulation: The process of binding the code and data together in a single unit

  Was this answer useful?  Yes

ajan

  • Aug 29th, 2007
 

You have given difference for the above question such as "Abstract class does not support Multiple Inheritance. Interface supports Multiple Inheriatnce". Can you provide me an example for this difference. If possible give me an real time example

  Was this answer useful?  Yes

Sandip mple:

  • Sep 9th, 2007
 

The main differece is
Abstraction: In this we can identify the specific properties and attributes of a pertricular class.
and exposing the data to the user ..........
example: A car, we can know or see that what it look lies in color , in the restpect to body of car but we can not know the fearchers they are using into the car


Encapsulation:
Binding the data and functions into the single unit.
i.e in the other way u can say that hiding the details from the others..

example :
same Car
in which we can see how the car is but when it is not working in that case i can not repaire it b'coz all the details of repairing are hidden from me ...

  Was this answer useful?  Yes

madhavi

  • Sep 11th, 2007
 

We are marking the class so that JVM recognize that class and perform certain action for that. ex: ref clone

  Was this answer useful?  Yes

the abstract class have  both abstract methods and concrete methods

in case of interface have only abstract methods.

we can not create instance for abstract class and an interface

  Was this answer useful?  Yes

Ok this is one of the common interview questions. Let me put this in simple words

when you use Interface :
There can be different classes which  show different behaviour. Like consider a case I want to put all animals in a tree structure. I have top most defination as Animal then we have birds, reptiles, mammals etc.. Now all these have different features from each other yet they show some common features like they move they eat food ... so i will use Animal as interface and define those features in the class that implements it.

when you use abstract class:
when i want to show a is-a type of realtion. now lets go further with the same example now I want to further classify my birds as those which can fly and those that cann't fly.. now this is specific to the birds in here i will go with abstract class.


  Was this answer useful?  Yes

mabhijit

  • Jan 11th, 2008
 

The Key difference b/t the Abstract class and the Interface is that :-

An abstarct class can be extended or used only in the same level of heirarchy, where as Interface can be implemented or used in a different level of heirarchy.

e.g. :-
If we want to create a common method for the Lion class and Cat class then we can create it as abstract because Lion and Cat are of the same family (Mean to say that they are in the same hierarchy).
But if we want to create a common method for the  Lion class and Car class then we can not create it in abstract class because both the class are in different hierarchy. So here we can create an interface for this.

  Was this answer useful?  Yes

AshaMahi

  • Apr 28th, 2008
 

A Programmer uses Abstract class to specify common features of all objects and abstract methods to when we have dfferent implementation for diff objects

Uses interface when he doesnot know how to implement the features

  Was this answer useful?  Yes

Priya Rajan

  • Jan 29th, 2009
 

1) Interface do not have Constructors whereas Abstract class have Constructors.
2)  A class that extends Abstract class but implements interface. 

  Was this answer useful?  Yes

purva15

  • Sep 24th, 2009
 

A abstract class define both abstract and non abstract methods but interface can have only abstract methods.

  Was this answer useful?  Yes

harihb

  • Jun 2nd, 2011
 

Versioning and default functionality.

Let's say that you have an abstract class called Human with a functionality walk(). Person A, B that extends abstract class gets the default functionality 'walk()'. If I have to add a new functionality 'run()', then I should use abstract class and not interface. If I use interface, then I have to implement run() in *all* the classes the implements Human interface and it will break the system. However, If I do in abstract class, then those classes that extend abstract class will implement it.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions