What is difference between abstract class and interface

Questions by apt_dpl

Showing Answers 1 - 39 of 39 Answers

There is no difference in the functionality of these two.The only difference is that a class cannot extend an abstract class if it already is extending some other class.An interface on the other hand can be implemented in any situation which makes them very powerful.Also user defined exceptions can be defined within an interface itself, which is not the case with an abstract class.

Praveen Kumar V

  • Mar 3rd, 2006
 

An Abstract Class can contain default Implementation, where as an Interface should not contain any implementation at all. An Interface should contain only definitions but no implementation. where as an abstract class can contain abstract and non-abstract methods. When a class inherits from an abstract, the derived class must implement all the abstract methods declared in the base class. an abstract class can inherit from another non-abstract class.

kotesh

  • Mar 5th, 2006
 

1. Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. Inteface is a Java Object containing method declaration and doesn't contain implementation. The classes which have implementing the Interfaces must provide the method defination for all the methods.2. Abstract class is a Class prefix wtih a abstract keyword followed by Class definaton. Interacace is a Interface which starts with interface keyword.3. Abstract class contatins one or more abstract methods. where as Interface contains all abstract methods and final declarations.4. Abstract class contains the method defination of the some methods. but Interface contains only method declaration, no defination provided.5. Abstract classes are useful in a situation that Some general methods should be implemented and specialization behaviour should be implemented by child classes. Interafaces are useful in a situation that all properties should be implemented we can use this scenario

n.shah

  • Mar 5th, 2006
 

diff 1 )  interface must always have by default public and abstract methods

while abstract class can have non abstract methods

diff 2 ) all the variables in interface are by default public static final while ion abstract class you may have instant variables

the main differences between an interface class and abstract class are

1.abstract classes must be given by keyword abstract,where as in interface class with key word interface

2.every method in interface class should be abstract,but this is not necessary in abstract class

3.by default what ever variables we decalre in interface are public static final,where as in abstract class they can be default and instance variables also

  Was this answer useful?  Yes

shankar

  • Apr 4th, 2006
 

Abstract Class is a class which must have at least one abstract method.Interface is a class which is having all abstract methods. By default interface methods are public abstract.

  Was this answer useful?  Yes

vara Rama Krishna Chadaram

  • Apr 24th, 2006
 

      Interface                             Abstract Class

---------------------------------------------------------------------------

(1)    Decelear with keyword                  (1)  Decelear with keyword  

         interface                                                Abstract.

(2)    It will not                               (2) Itwill may or may            contain      Constructor.                                     Constructor.

(3)  By default all methods                   (3) It may contact Abstract

   are Public AbstractMethods.                  methods and non

                                                          Abstract methods.

(4) it will contain only                          (4) It will final variables and

     final variables,                                    Instances Variables.

  Was this answer useful?  Yes

chandrahasa reddy

  • Oct 22nd, 2006
 

Dear Greekintreview.com,

 everyday i searched this website regarding java information.i got sufficient information diff between interfaces and classes given answer for Mr Koteswara Reddy.

thanks for koteswara reddy

  Was this answer useful?  Yes

Dharmendra

  • Oct 27th, 2006
 

Abstract Class means Blueprint of the model.

Interface means functionality of the model(Blueprint) if the class implements one(an interface).

Using Abstract class we can take n number of instances. Like using the Blueprint of a Car
we can take n number of similar cars. But we cannot use the Bluprint or model to drive the
car.That is the meaning of abstract. So, Car is the Blueprint and Ford Car is the derived class from car which uses all the
properties of class and some more properties which is specific to Ford.

Frog is a derived class from Mammal.

When it lives on land it implements the function of hopping.

when it lives on water it implements the function of swimming.

So we find that frog uses both methods hopping and swimming. So it is better to declare
in some interface so that any frog which implements the interface will define its own
way of transportation.

So let me create an Interface called Amphibian and give two method Swimming() and hopping().
It looks like

public interface Amphibian{

public void swim();
public void hop();
}

Let we define the Frog class as

public class Frog implements Amphibian{

public void swim(){

//Define your words
}

public void hop(){

//Define your words
}

}

The above class Frog when implements the interface Amphibian it has to define the methods
declared in the interface Amphibian. It can be an empty method. But it should be defined.

The use of an interface is any class which wants to use some remote functionalities can
implement that particular interface and can perform the task like man using Parachute to
fly in air.

- Compiled by Dharmendra

perireddy

  • Oct 30th, 2006
 

abstract class contains both final and instance variablea .afinalvariable is also ainstance variable then what is the difference mr.koteswara reddy

  Was this answer useful?  Yes

SrideviMohan

  • Nov 1st, 2006
 

   If the functionalites(declaration and definition) is same for all classes in the project and that is mandatory that time we use Abstract class.

If the declaration is same but the implementation logic is going  differ in class to class that time we use Interface

  Was this answer useful?  Yes

atul931

  • Feb 28th, 2007
 

The difference between Interface and Abstract class is,

1:In abstarct class you can use Final and Instance Variable.
2:In abstarct class you can use Abstract method or non-abstract method,  But in Interface, only Abstarct method can be used.

  Was this answer useful?  Yes

S.JEELAN BASHA

  • Mar 6th, 2007
 

It's very nice to see such a difficult topic in a simple and effective manner.
Thank Q very much..

  Was this answer useful?  Yes

sourav

  • May 9th, 2007
 

Abstract class can have constructor but interface can not have constructor.

Abstruct class constructor run when one of its subclass constructor execute.

  Was this answer useful?  Yes

Binesh Speridian

  • May 25th, 2007
 

There is no need to declare abstract method in Abstract classes

  Was this answer useful?  Yes

1. Abstract class contain both abstract and?nonabstract?? methods.
But?interface? can only contain abstarct methods.
2. If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change. But, If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method.
3. A?class?can implement? more than one? interface?but?can?extend?only one abstarct class.

  Was this answer useful?  Yes

gyoganandam

  • Mar 25th, 2008
 

1. Abstract  class cannot instantiate must be inherited
2. abstract class have one abstract method.
3. interface have all abstract methods.

  Was this answer useful?  Yes

sankara123

  • Jul 22nd, 2008
 

) Abstract class defines few or none of the methods, but 
interface defines all the methods..
2) Abstract classes should have subclasses else that will
be useless..
Interfaces must have implementations by other classes else
that will be useless
3)only an interface can extend another interface, but any
class can extend an abstract class..
4)All variable in interfaces are final by default

  Was this answer useful?  Yes

All the answers are fine.But there is a very important difference. In the special case, if you have a abstract class, and full hierarchy of subclasses under that, suppose there is class A which is abstract. And Class B, Class C, Class D are extending that. so if you make any change in abstract class, you have to recomplile all the subclasses while in the case of interface, you have to simply compile just interface and you are done.

  Was this answer useful?  Yes

Abstract :
1) There won't be any code for methods.
2) We got to give our own code.
3) Abstract class was uncompleted.

Interface:
1)
Compulsory write the code in all methods.
2) More "Parent" classes implements in one Child class.
3) You got to implement the Parent class to child class and can overwrite the methods in the Parent class.

  Was this answer useful?  Yes

Abstract class is not fully implemented class. The main purpose of abstract class is providing abstraction.
1)It may be or may not be contains abstract methods.it may has defined methods also.
2) It contains any kind of fields.

3) Subclass extends only one abstract class (Bcoz Java doesn't support multiple inheritance).
Interface is included in Java to support multiple inheritance.
1) It should contain abstract methods only.
2) It contains only final fields.
3) Subclass may implements any no of interfaces.

Correct me if I am wrong

  Was this answer useful?  Yes

ariramk

  • Jan 25th, 2010
 

Interface - loosely coupled

  • All the Methods must be public.
  • By Default. variable/constants are static final.
Abstract - tightly coupled
  • Can have private/public methods

  Was this answer useful?  Yes

Chinna1983

  • Sep 25th, 2010
 

Abstraction:
1) All the fields are abstract fields.  If any one of the method is abstract methods that class is going to AbstractClass.
2) Unable to create the object for Abstract class.
3) Abstract class having concrete methods (non abstract methods) and abstract methods.
4) In the future you want to add the some
feature (implementation code) to all derived classes for the particular AbstractClass Just place that feature (implemented code (non abstract method)) into the abstract class.
You cannot do this in interface.

Interface:
1) Only given
structure. All the methods is non implemented methods.
2) Unable to create the object.

  Was this answer useful?  Yes

"Abstract Class is a class which must have at least one abstract method.": NOT TRUE!!

Try it:
public abstract class b
{
    public void methodOne()
    {
    }
}
compiles just fine.

  Was this answer useful?  Yes

1. Interface methods implementation should need to be given in the sub class but not in the case of abstract class.
2. Abstract class have the common implementation methods in it, which can be used again in the sub class. Whereas in interface implementation for the methods should need to given in the sub class(Mandatory).

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

mlazur

  • Mar 24th, 2011
 

There is a difference in functionality.  While subclasses can only use one inherited class, they can use multiple interfaces.

  Was this answer useful?  Yes

There are quite a few known differences:

Extensibility:

Interface: If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

Abstract class: If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

Attributes:

Interface: No fields can be defined in interfaces

Abstract class: can have fields and constants defined

Access modifiers

An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public

Abstract class can contain access modifiers for the subs, functions, properties

Implementation

An interface cannot provide any code, just the signature.

Abstract class can provide complete, default code and/or just the details that have to be overridden.

  Was this answer useful?  Yes

IKRAM RANA

  • Jul 30th, 2011
 

abstract class are the partially implementation of abstraction.interfaces are the fully implementation of abstraction.abstract class can represent both behavior and properties of an object.interfaces can represents only the behavior of an object.In abstract class we can have normal function but in interface we can not have normal function.

  Was this answer useful?  Yes

Srinivasulu.D

  • Aug 8th, 2011
 

The differences between abstract class an interface as fallows:

1. Abstract class has the constructor, but interface doesn’t.

2. Abstract classes can have implementations for some of its members (Methods), but the interface can’t have implementation for any of its members.

3. Abstract classes should have subclasses else that will be useless..

4. Interfaces must have implementations by other classes else that will be useless

5. Only an interface can extend another interface, but any class can extend an abstract class..

6. All variable in interfaces are final by default

7. Interfaces provide a form of multiple inheritance. A class can extend only one other class.

8. Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.

9. A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.

10. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.

11. Accessibility modifier(Public/Private/internal) is allowed for abstract class. Interface doesn’t allow accessibility modifier

12. An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.

13. An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property’s signature but no implementation.

14. Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.

15. Abstract scope is upto derived class.

16. Interface scope is upto any level of its inheritance chain.

  Was this answer useful?  Yes

maddy

  • Aug 20th, 2011
 

Code
  1. span style="color: #339933;">#region try block

  2. "*.*", SearchOption.AllDirectories);

  3.  

  4.                 #region loop thru for all diretories&subdiretories

  5. #region if file type=doc

  6. ".doc""DOC"//System.IO.File.Copy(sourcePath, @destFile , true);"DOC"//System.IO.File.Copy(sourcePath, @destFile, true);

  7.                         }

  8.  

  9.                     }

  10.                     #endregion

  11.  

  12.                     #region if file type=rtf

  13. ".rtf""RTF""RTF"#endregion

  14.                     #region if file type=pdf

  15. ".pdf""PDF""PDF"#endregion

  16.  

  17.                 }

  18.                 #endregion

  19.  

  20.                 #region code to write data on xml file

  21.                 try

  22.                 {

  23.                     //code to write data on xml file"*.doc""*.rtf""*.pdf*""XMLFile.xml"//row.BeginEdit();

  24.                     //row[0] += "'" + documents ;

  25.                     //row.EndEdit();

  26.                     ds.WriteXml(Server.MapPath("xmlfile.xml"));

  27.                     //lblmsg.Text = "record inserted";

  28.  

  29.                 }

  30.                 catch (Exception ex)

  31.                 {

  32.                     lblmsg.Text = "Error: "//Server.Transfer("XMLFile.xml");

  33.                 Response.Redirect("XMLFile.xml");

  34.                 #endregion

  35.             }

  36.         }

  37. #endregion

  38.         #region Catch block

  39.         catch (Exception ex)

  40.         {

  41.             lblmsg.Text = "Error:"#endregion

  42.  

  43.     }

  44.  

  45. -------------------------------------------------------------------------------------------------

  46. <%@ Page Language="C#""true" CodeFile="MoveFiles.aspx.cs" Inherits="MoveFiles""-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  47.  

  48. <html xmlns="http://www.w3.org/1999/xhtml">

  49. <head runat="server">

  50.     <title>Copy Files</title>

  51.     <style type="text/css">

  52.         .style1

  53.         {

  54.             width: 32%;

  55.         }

  56.         .style2

  57.         {

  58.             width: 155px;

  59.         }

  60.     </style>

  61.     <script type ="text/javascript" src ="valid.js""frmsample" runat="server">

  62.     <div align="center">

  63.    

  64.         <br />

  65.         <br />

  66.         <br />

  67.         <br />

  68.         <br />

  69.         <br />

  70.    

  71.         <table class="style1" style="border: thin double #0000CC">

  72.             <tr style="background-color: #00CCFF">

  73.                 <td colspan="2"

  74.                     style="font-family: Arial; font-size: large; font-style: normal; color: #0000CC; text-decoration: underline">

  75.                     Copy Files</td>

  76.             </tr>

  77.             <tr>

  78.                 <td align="right" style="background-color: #00FFFF">

  79.                     <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Source"></asp:Label>

  80.                 </td>

  81.                 <td class="style2">

  82.                     <asp:TextBox ID="txtsoure" runat="server" Width="192px"></asp:TextBox>

  83.                 </td>

  84.             </tr>

  85.             <tr>

  86.                 <td align="right" style="background-color: #66FFFF">

  87.                     <asp:Label ID="Label2" runat="server" Font-Bold="True" Text="Destination"></asp:Label>

  88.                 </td>

  89.                 <td class="style2">

  90.                     <asp:TextBox ID="txtdestination" runat="server" Width="193px"></asp:TextBox>

  91.                 </td>

  92.             </tr>

  93.             <tr>

  94.                 <td style="background-color: #66FFFF">

  95.                      </td>

  96.                 <td align="left" class="style2" style="background-color: #66FFFF">

  97.                     <asp:Button ID="btnsubmit" runat="server" Font-Bold="True"

  98.                         onclick="btnsubmit_Click" OnClientClick ="return valids()" Text="Submit"

  99.                          />

  100.                 </td>

  101.             </tr>

  102.             <tr style="background-color: #00CCFF">

  103.                 <td colspan="2">

  104.                     <asp:Label ID="lblmsg" runat="server" Font-Bold="True"'txtsoure').value=="")

  105.         {

  106.           alert("Please Enter source"'txtsoure''txtdestination').value=="")

  107.         {

  108.           alert("Please Enter destination"'txtdestination''source&destination must not be same''txtsoure').style.background='linen''txtdestination').style.background='linen''txtsoure'//       script for source path checking'txtsoure'"Windows""/")

  109.                 {

  110.                         alert("Enter Valid source path""/""/")

  111.                 {

  112.                         alert("Enter Valid source path""""""/""/""Enter Valid source path""""Enter Valid source path"// script for destination path checking'txtdestination'"Windows""/")

  113.                 {

  114.                         alert("Enter Valid Destination path""/""/")

  115.                 {

  116.                         alert("Enter Valid Destination path""""""/""/""Enter Valid Destination path""""Enter Valid Destination path"


  Was this answer useful?  Yes

udaiveer singh

  • Aug 23rd, 2011
 

the main diff between the java interface and abstract class is that java interface have only abstract methods where as java abstract class can have abstract as well as instance methods also.

  Was this answer useful?  Yes

Brijendra(Xavient)

  • Oct 5th, 2011
 

1.Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
2.Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
3.Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
4.Java interface should be implemented using keyword implements; A Java abstract class should be extended using keyword extends.
5.An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
6.A Java class can implement multiple interfaces but it can extend only one abstract class. 7.Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
8.In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

  Was this answer useful?  Yes

kuldeep khaware

  • Oct 17th, 2011
 

The only difference is that a class cannot extend an abstract class if it already is extending some other class.An interface on the other hand can be implemented in any situation which makes them very powerful.

Code
  1. span style="font-style: italic;">//Interface

  Was this answer useful?  Yes

sakthi

  • Jan 28th, 2012
 

Interface: is like a class but it will not contain code implementation in interface method, and also it may be final or static, we can use multiple inheritance here.

Abstract: is like a super class, it may partially implemented function, We can use only single inheritance.

  Was this answer useful?  Yes

Ambresh Sharma

  • Feb 5th, 2012
 

1.abstract class contain concrete as well as non-concrete method.But Interface contains concrete method.
2.abstract class method cant be defined as static,private,final,synchronized where as interface method method by default public and abstract.
3.In abstract class you can define static method but In Interface you cant define.
4.you can define constructor of the abstract class but in case of Interface their you cant do that.

  Was this answer useful?  Yes

sharoon

  • Sep 26th, 2012
 

In a very simple and high level, Interface is something to tell a guy, Hey X, you should to all these if you need me whereas abstract class tells Hey X, you should do this alone need not to disturb others.

  Was this answer useful?  Yes

UMA

  • Jan 22nd, 2013
 

polymorphism are mainly two types
static polymorphism(corresponding method will bind at the
time of compiling)
dynamic polymorphism(corresponding method will bind at the
run time)

Static polymorphism is used the concept of early binding or we can say
compile time binding where as dynamic polymorphism used the concept of
late binding or run time binding.

method overloading would be an example of static polymorphism
whereas overriding would be an example of dynamic polymorphism.

BCoz,in case of overloading,at compile time the compiler knows which method to link to the call .
However,it is determined at runtime for dynamic polymorphism.
9 months ago

  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